1

我对 div 切换有一点问题。在我的页面中,我有两个主要的 div,一个是 box,另一个是 box2。

当用户首先打开我的页面时,只有框是可见的,但是当他/她点击 box2 时,它会向下滑动并显示。

用户切换 box2 后,在页面重新加载时该 div 必须打开,但我不知道如何为此设置 cookie。

请在此处查看(http://jsfiddle.net/wasimkazi/fauNg/18/)

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");

widget2.hide();
box2.click(function() {
$(this).next(widget2).slideToggle("fast", function() {});
});

var inner = $(".inner"),
box = $(".box");

inner.hide();
box.click(function() {
$(this).next(inner).slideToggle("fast", function() {
  });
});
4

1 回答 1

1

You can use something like this to store the status of the box and then use getcookie to check the status on page reload and then toggle it accordingly

box2.click(function() {
    $(this).next(widget2).slideToggle("fast", function() {
        var flag = ($(this).css("display") == 'none')?'off':'on';
        setCookie('box2', flag);
    });
});

And then on page reload check for the status/value of the box and then toggle accordingly. something like

if(getCookie('box2') == 'on'){
    box2.show();
}else{
    box2.hide();
}

Not the exact solution but jus giving you some way you will be able to handle it

if you have a doubt regarding whether your setcookie and getcookie are correct, you can refer this link on quirksmode for simple implementation of the cookies

于 2012-07-31T07:57:50.493 回答