Jquery cookie plugin - how to set cookie for show/open element
When I navigate away from page to another one I need to remember whitch block is open This is colapsible mavigation menu.The div 'block' content has links inside.
<script type="text/javascript">
<!--//--><![CDATA[//><!--
$(document).ready(functio开发者_Python百科n () {
$('.acc_container').hide();
$('.acc_trigger:first').addClass('active').next().show();
$('.acc_trigger').click(function(){
if( $(this).next().is(':hidden') ) {
$('.acc_trigger').removeClass('active').next().hide();
$(this).toggleClass('active').next().show();
}
return false;
});
});
//--><!]]>
</script>
<h2 class="acc_trigger"><a href="#">Messages</a></h2>
<div class="acc_container">
<div class="block">content1 </div>
</div>
<h2 class="acc_trigger"><a href="#">Categories</a></h2>
<div class="acc_container">
<div class="block">content2 </div>
</div>
.....
<h2 class="acc_trigger"><a href="#">Clients</a></h2>
<div class="acc_container">
<div class="block">content10 </div>
</div>
Try this:
$(document).ready(function () {
$('.acc_container').hide();
$('.acc_trigger')
.eq( $.cookie('activeTrigger'))//<!-- read index from cookie
.addClass('active').next().show();
$('.acc_trigger').click(function(){
$.cookie('activeTrigger',$('.acc_trigger').index(this));//<!-- set the cookie
if( $(this).next().is(':hidden') ) {
$('.acc_trigger').removeClass('active').next().hide();
$(this).toggleClass('active').next().show();
}
return false;
});
});
It stores onclick the index of the clicked item inside $('.acc_trigger')
to a cookie , later it uses this index to select this item via $().eq()
精彩评论