2010-09-13 16 views
5

Bir sayfada belirli bir div gösterme ve gizleme konusunda harika bir eğitici buldum. Kodun iyi çalıştığını biliyorum, ancak genişletmek isterim ki, sayfa yüklerinde show/hide hatırlanır. Ben ise gerçek kod yazma bildiği edersen .. Bir çözüm jQuery çerezi için cevap oldu baktım .. İşte akım pasajı var:Gizleme/gösterme öğesini hatırlamak için jQuery cookie.js'yi kullanma?

<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> 

Ben çerez eklemek nasıl herhangi bir fikir Kullanıcıdan seçimi hatırlıyor musunuz? Ben hala oldukça kolay olmalı genel olarak jQuery/Javascript :) önceden

Teşekkür :)

cevap

8

kavramak çalışıyorum çünkü bir kod örneği çok iyi olurdu. Eğer div gibi bazı kod eklemek gösterdiğinizde:

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

... ve div gizlemek zaman:

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

... ve sonra kod üstündeki Elinizdeki nerede :

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

... olarak değiştirin:

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

Ya da, alternatif olarak:

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

:)

+0

Teşekkür !! Bazı yardım ve karışıklık ile bu mükemmel çalışmak için var! –

İlgili konular