2010-09-13 11 views
5

Ho trovato un ottimo tutorial su come mostrare e nascondere un certo div su una pagina. Ho ottenuto il codice funzionante, ma mi piacerebbe estendere è che lo show/hide è ricordato sui carichi di pagina. Ho cercato una soluzione jQuery cookie è stato la risposta .. se avessi saputo come scrivere il codice che è .. Ecco il frammento di corrente:Utilizzare jQuery cookie.js per ricordare l'elemento 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> 

Qualsiasi idea di come posso aggiungere i biscotti per ricorda la selezione dall'utente? Un esempio di codice sarebbe grande visto che sto ancora cercando di capire jQuery/Javascript in generale :)

Grazie in anticipo :)

risposta

8

Dovrebbe essere abbastanza facile. Quando si mostra il div aggiungere un po 'di codice come:

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

... e quando si nasconde il div:

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

... e poi nella parte superiore del codice in cui hai :

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

... modificarla in:

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

Oppure, in alternativa:

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

:)

+0

Grazie !! con un po 'di aiuto e scherzi, ho fatto in modo che funzionasse perfettamente! –