我有一个菜单,如果页面重新加载,当用户单击关闭或打开时,我希望状态打开/关闭状态保持不变。
我正在使用cookie插件,我马上就到了,但是我在设置关闭cookie以记住关闭状态时遇到了困难,打开的cookie仍然存在。
$(document).ready(function() {
// Open / Close Panel According to Cookie //
if ($.cookie('filtermenu') === 'open'){
$('.filter').show(); // Show on Page Load / Refresh without Animation
}
else {
}
if ($.cookie('filtermenu') === 'close' || $.cookie('filtermenu') === null){
$('.filter').hide(); // Show on Page Load / Refresh without Animation
}
else {
}
// Toggle Panel and Set Cookie //
$('#filter-menu').click(function(){
$('.filter').slideToggle('fast', function(){
if ($('#filter-menu').is(':hidden')) {
$.cookie('filtermenu', 'close', { expires: 30 });
} else {
$.cookie('filtermenu', 'open');
}
});
return false;
});
});有人能看看我做错了什么吗。
谢谢。
更新:对不起,现在我有了这个,但是它仍然没有被关闭,我是否错误地使用了对象?
Ok, so now I have this, but the menu still doesn't stay closed.
$(document).ready(function() {
// Open / Close Panel According to Cookie //
if ($.cookie('filtermenu') === 'open'){
$('.filter').show(); // Show on Page Load / Refresh without Animation
}
else {
}
if ($.cookie('filtermenu') === 'close' || $.cookie('filtermenu') === null){
$('.filter').hide(); // Show on Page Load / Refresh without Animation
}
else {
}
// Toggle Panel and Set Cookie //
$('#filter-menu').click(function(){
$('.filter').slideToggle('fast', function(){
var now = new Date();
var time = now.getTime();
time -= 60 * 1000;
now.setTime(time);
$.cookie('filtermenu', 'open', {expires: now});
if ($('#filter-menu').is(':hidden')) {
$.cookie('filtermenu', 'close', { expires: 30 });
} else {
$.cookie('filtermenu', 'open');
}
});
return false;
});
}); 发布于 2014-02-26 15:04:10
在设置关闭cookie之前,您需要重新设置打开的cookie,其过期日期要大于当前时间。这样,打开的cookie就会立即过期,剩下的就是关闭的cookie了。
更新-获取时间
您已经询问了如何在JS中获得当前时间,下面是一些示例代码:
var now = new Date();
var time = now.getTime();
time -= 60 * 1000;
now.setTime(time);这将创建一个名为Date的新的now对象,并使用getTime()从它中提取时间。然后我们减去一分钟的时间(时间以毫秒为单位,所以是60 * 1000),然后将日期设置为该时间。您现在可以使用此Date对象来设置cookie的过期时间!
https://stackoverflow.com/questions/22045221
复制相似问题