题
我要做的是让侧边栏打开和关闭,这很好,但是我也希望cookie在浏览网站时记住侧边栏状态,并在返回页面上时
即:如果打开开放,如果关闭关闭:
$(document).ready(function($) {
$('a#side').click(function(){
$('#sidebar').toggle();
$('a#side').text($(this).text() == 'Show' ? 'Hide' : 'Show');
$.cookie('side_cookie', 'value');
return false;
});
if($.cookie('side_cookie')) {
$('#sidebar').hide();
} else {
$('#sidebar').show();
}
});
上面的当前代码只是记住它是否已关闭并保持关闭,直到会话结束,因此您必须在每次返回页面时都打开它。
如果您关闭侧边栏,则可以在vbulletin.com/forum/上看到我试图实现的目标的一个示例,然后返回主页时浏览论坛,它仍然关闭,Visa Versa。
任何帮助都将受到赞赏
解决方案 3
好的,但我对它进行了排序,它的代码要多于我想要的。
$('a#hide').click(function(){
$('a#hide,#sidebar').hide();
$('a#show').show();
$.cookie('side_cookie_hidden', 'hidden');
$.cookie('side_cookie_show', null);
return false;
});
$('a#show').click(function(){
$('a#show').hide();
$('a#hide,#sidebar').show();
$.cookie('side_cookie_show', 'show');
$.cookie('side_cookie_hidden', null);
return false;
});
if($.cookie('side_cookie_hidden')) {
$('a#show').show();
$('a#hide,#sidebar').hide();
} else {
$('a#show').hide();
$('a#hide,#sidebar').show();
}
其他提示
我认为问题是如果条件($。cookie('side_cookie'))。尝试这个:
$(document).ready(function($) {
$('a#side').click(function(){
$('#sidebar').toggle();
$('a#side').text($(this).text() == 'Show' ? 'Hide' : 'Show');
$.cookie('side_cookie', $(this).text());
return false;
});
if($.cookie('side_cookie')=='Hide') {
$('#sidebar').hide();
} else {
$('#sidebar').show();
}
});
您必须检查cookie的价值,而不是其存在。另外,您正在硬编码cookie的价值 value
. 。将其更改为 show
或者 hide
.
// when it is shown
$.cookie('side_cookie', 'show');
// when it is hidden
$.cookie('side_cookie', 'hide');
并检查cookie的价值
if($.cookie('side_cookie') === "show") {
// code for showing side bar
}
else {
// code for hiding the side bar
}
不隶属于 StackOverflow