2014-09-22 95 views
0

我有这段代码,只要我重新加载或刷新页面,该代码就可以工作,但可以返回到初始关闭状态。即使在页面重新加载之后,我也希望保留最后的切换状态似乎无法解决这个问题。Slidetoggle在页面刷新时保留当前状态

我的代码是:

var $answers = $('.contentbar'); 
$(".titlebar").click(function() { 
    var $ans = $(this).next(".contentbar").stop(true).slideToggle(500); 
    $answers.not($ans).filter(':visible').stop(true).slideUp(); 
}) 
+4

使用'localStorage'或'cookie'来保存状态。 – Regent 2014-09-22 12:56:13

回答

0

有一个叫jQuery.cookie插件。使用该功能,您可以检查是否已有用户设置的手风琴,并根据偏好设置,默认情况下可以显示/隐藏东西。

var $answers = $('.contentbar'); 
$(".titlebar").click(function() { 
    if (!$.cookie('contentbar')) 
     var $ans = $(this).next(".contentbar").stop(true).slideToggle(500); 
    else 
    { 
     var $ans = $($.cookie('contentbar')).next(".contentbar").stop(true).slideToggle(500); 
     $.cookie('contentbar', this); 
    } 
    $answers.not($ans).filter(':visible').stop(true).slideUp(); 
});