2011-01-16 45 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