2012-07-31 38 views
0

我有一个滑动菜单,其中滑动了一些div。我们如何获得cookie中的选定div

在页面刷新或更改后,我希望该菜单的状态,因为它是为了我使用Cookie。

在那个cookie中,无论我得到什么值都会影响整个班级,所以我的所有孩子都会出现在屏幕上而不是选定的div上。

我做了一个jsfiddle也审查。检查here

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); 
}); 
});​ 

回答

0

这一个现在工作很完美。 demo

$(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); 
});​ 
相关问题