我有一个滑动菜单,可以在其中滑动一些 div。
在页面刷新或更改之后,我想要该菜单的状态,因为我使用了 cookie。
在那个cookie中,我得到的任何值都会影响整个班级,所以我所有的孩子都会出现在屏幕上,而不是那个选定的div。
我也制作了一个 jsfiddle 以供审查。在这里检查
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays === null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
var widget2 = $(".widget2"),
box2 = $(".box2");
var inner = $(".inner"),
box = $(".box");
widget2.hide();
inner.hide();
if(getCookie('box2') == 'on'){
widget2.show();
//alert('hi');
}else{
widget2.hide();
//alert('hiiiiii');
}
if(getCookie('box') == 'on'){
//widget2.show();
inner.show();
alert('hi');
}else{
inner.hide();
//alert('hiiiiii');
}
box2.click(function() {
$(this).next(widget2).slideToggle("fast", function() {
var flag = ($(this).css("display") == 'none')?'off':'on';
setCookie('box2', flag);
});
});
var inner = $(".inner"),
box = $(".box");
box.click(function() {
$(this).next(inner).slideToggle("fast", function() {
var flag = ($(this).css("display") == 'none')?'off':'on';
setCookie('box', flag);
});
});