2010-09-13 52 views
5

我發現了一個關於如何在頁面上顯示和隱藏某個div的好教程。我得到的代碼工作正常,但我想擴大是顯示/隱藏記得在頁面加載。我找了一個解決方案jQuery的餅乾是答案..如果我知道如何編寫實際的代碼,是...以下是當前片斷:使用jQuery cookie.js記住hide/show元素?

<script type="text/javascript"> 
jQuery(document).ready(function() { 
// hides the group_desciption as soon as the DOM is ready 
// (a little sooner than page load) 
    jQuery('#group_desciption').hide(); 
// shows the group_desciption on clicking the noted link 
    jQuery('a#slick-show').click(function() { 
jQuery('#group_desciption').show('slow'); 
return false; 
    }); 
// hides the group_desciption on clicking the noted link 
    jQuery('a#slick-hide').click(function() { 
jQuery('#group_desciption').hide('fast'); 
return false; 
    }); 
// toggles the group_desciption on clicking the noted link 
    jQuery('a#slick-toggle').click(function() { 
jQuery('#group_desciption').toggle(400); 
return false; 
    }); 
});</script> 

任何想法,我怎麼可以添加到餅乾記得用戶的選擇?一個代碼示例將是巨大的,因爲我仍然試圖抓住的jQuery/JavaScript的一般:)

感謝提前:)

回答

8

應該很容易。當你告訴股利像添加一些代碼:

jQuery.cookie('show_desc', 'yep'); 

...當你隱藏的div:

jQuery.cookie('show_desc', 'nope'); 

...然後在你的代碼的頂部,你就有了:

jQuery('#group_desciption').hide(); 

...將其更改爲:

var shouldShow = jQuery.cookie('show_desc') == 'yep'; 
if(shouldShow) { jQuery('#group_desciption').show(); } 
else {    jQuery('#group_desciption').hide(); } 

或者,或者:

jQuery('#group_desciption')[jQuery.cookie('show_desc') == 'yep' ? 'show' : 'hide'](); 

:)

+0

謝謝!與一些幫助和搞亂我得到這個完美的工作! – 2010-09-14 16:01:09