0

我有一个滑动菜单,可以在其中滑动一些 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);
 });
});​ 
4

1 回答 1

0

这个现在工作完美。演示

$(document).ready(function() {

$(".widget2").hide();
$(".inner").hide();

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");
var box2 = $(".box2");
if (getCookie('box2cooki')) {
    var id = getCookie('box2cooki');
    //alert('#'+id);
  $('#' + id).next(widget2).slideDown(100);
  $('#' + id).closest('div').next().find('.inner').slideDown(100);
} else {
    $(".widget2").hide();
    $(".inner").hide();
}


box2.click(function() {
    $(this).next(widget2).slideToggle(200);
    var box2ID = $(this).attr('id');
    setCookie('box2cooki', box2ID);
    //alert(box2ID);
});
//$(".inner").hide();
$(".box").click(function() {
    $(this).next(".inner").slideToggle(200);
});

//alert(box2ID);
});​
于 2012-08-06T12:37:33.990 回答