2012-12-17 183 views
0

我有一个按钮,切换我的页脚。当前代码从页脚关闭开始。我的问题是我如何使默认状态打开?我已经尝试过几种配置,但不知道什么是正确的。当我在浏览器上检查cookie时,默认状态为'隐藏'。Jquery切换按钮,并关闭状态

// Toggle Button 

$(document).ready(function() { 
var button = $('.toggle'); 

//check the cookie when the page loads 
if ($.cookie('currentToggle') ==='visible') { 
    togglePanel(button, false); 
} 
    else { 
    togglePanel(button, true); 

} 

//handle the clicking of the show/hide toggle button 
button.click(function() { 
    //toggle the panel as required, base on current state 
    if (button.text() === "-") { 
     togglePanel($(this), true); 
    } 
    else { 
     togglePanel($(this), false); 

    } 
}); 

}); 

function togglePanel(button, show) { 

var panel = $('footer'); 

if (show) { 
    panel.slideUp('slow'); 
    button.text('+'); 
    $.cookie('currentToggle', 'hidden', { path: '/' }); 

} 
else { 
    panel.slideDown('slow'); 
    button.text('-'); 
    $.cookie('currentToggle', 'visible', { path: '/' }); 

} 
} 
+0

Cookie的默认状态是它根本不存在。你需要检查这个,然后实现你想要的默认值。 – Barmar

回答

0

我想你只需要这一行处理的情况下,因为没有Cookie设置:

if (!$.cookie('currentToggle')) 
    $.cookie('currentToggle', 'visible'); 

把它之前//check the cookie when the page loads

或者,更简单,扭转你的当您检查时的逻辑:

if ($.cookie('currentToggle') === 'hidden') { 
    togglePanel(button, true); 
} else { 
    togglePanel(button, false); 
} 
+0

非常感谢 - 设置了默认状态。 – Claud