2011-01-16 103 views
1

我想要做的是有一个侧边栏切换打开和关闭其工作正常,但我也希望cookie来浏览网站时,请记住侧栏状态并返回到其页面上jQuery和cookie的状态

时即:如果关闭关闭,如果打开是开放的:

$(document).ready(function($) { 
$('a#side').click(function(){ 
    $('#sidebar').toggle(); 
$('a#side').text($(this).text() == 'Show' ? 'Hide' : 'Show'); 
    $.cookie('side_cookie', 'value'); 
return false; 
}); 
if($.cookie('side_cookie')) { 
$('#sidebar').hide(); 
    } else { 
$('#sidebar').show(); 
    } 
}); 

当前的代码上面只是会记住它被关闭并保持关闭,直到会议结束,所以你要切换它每次返回页面时打开。 ..

一个例子,即时通讯尝试achi可以在vbulletin.com/forum/上看到前夕,如果您关闭他们的侧边栏,然后浏览论坛,当您返回主页面时仍然关闭,反之亦然。

任何帮助表示赞赏

回答

0

好THX球员,但我得到它排序的价值,它的很多更多的代码,那么我想作为其.show()的很多/。隐藏()而不是.toggle()的击打它的工作原理sofor现在会做:

$('a#hide').click(function(){ 
    $('a#hide,#sidebar').hide(); 
    $('a#show').show(); 
    $.cookie('side_cookie_hidden', 'hidden'); 
    $.cookie('side_cookie_show', null); 
return false; 
}); 

$('a#show').click(function(){ 
    $('a#show').hide(); 
    $('a#hide,#sidebar').show(); 
    $.cookie('side_cookie_show', 'show'); 
    $.cookie('side_cookie_hidden', null); 
return false; 
}); 

if($.cookie('side_cookie_hidden')) { 
$('a#show').show(); 
$('a#hide,#sidebar').hide(); 
    } else { 
$('a#show').hide(); 
$('a#hide,#sidebar').show(); 
} 
0

我认为这个问题是如果条件如果($。饼干( 'side_cookie'))。 试试这个:

$(document).ready(function($) { 
$('a#side').click(function(){ 
    $('#sidebar').toggle(); 
$('a#side').text($(this).text() == 'Show' ? 'Hide' : 'Show'); 
    $.cookie('side_cookie', $(this).text()); 
return false; 
}); 
if($.cookie('side_cookie')=='Hide') { 
$('#sidebar').hide(); 
    } else { 
$('#sidebar').show(); 
    } 
}); 
+0

THX Cyber​​nate但不工作:( – Dizzi

0

你必须检查Cookie,而不是其存在的价值。此外,您还将Cookie的价值硬编码为value。将其更改为showhide

// when it is shown 
$.cookie('side_cookie', 'show'); 

// when it is hidden 

$.cookie('side_cookie', 'hide'); 

和检查Cookie

if($.cookie('side_cookie') === "show") { 
    // code for showing side bar 
} 
else { 
    // code for hiding the side bar 
} 
+0

那是不是Cyber​​nate的例子表明..he tryed使用$(本)的.text(? )来设置cookie值,但它没有工作....如果它不是你可以举个例子,因为这是第一次使用cookies thx。 – Dizzi