How To Make Jquery Submenu Stay Visible After It Is Clicked?
I have read similar posts but as I am not very good with jquery I need more specific help with my menu.
<div id="nav"><!--// START NAV //-->
<dl id="nav">
<dt class="nav"><b>ONE</b></dt>
<dd>
<ul>
<li><a href="/A">A</a></li>
<li><a href="/B">B</a></li>
<li><a href="/C">C</a></li>
</ul>
&开发者_JAVA百科lt;/dd>
<dt class="nav"><b>TWO</b></dt>
<dd>
<ul class="nav">
<li><a href="/D">D</a></li>
<li><a href="/E">E</a></li>
<li><a href="/F">F</a></li>
</ul>
</dd>
</dl>
</div><!--// END NAV //-->
Here is the JS I am using.
<script type="text/javascript">
$(document).ready(function(){
if($("#nav")) {
$("#nav dd").hide();
$("#nav dt.nav b").click(function() {
if(this.className.indexOf("clicked") != -1) {
$(this).parent().next().slideUp(200);
$(this).removeClass("clicked");
}
else {
$("#nav dt.nav b").removeClass();
$(this).addClass("clicked");
$("#nav dd:visible").slideUp(200);
$(this).parent().next().slideDown(500);
}
return false;
});
}
});
</script>
Thanks
The menu hides due to pagerefresh. You can avoid it, by adding click handler, that returns false. Add this code to your ready
handler:
$("#nav dd ul a").click(function(){
return false;
})
Here is example.
Update:
You can store selected url in cookies. Here is sample:
$(document).ready(function() {
if ($("#nav")) {
$("#nav dd").hide();
$("#nav dt.nav b").click(function() {
if (this.className.indexOf("clicked") != -1) {
$(this).parent().next().slideUp(200);
$(this).removeClass("clicked");
}
else {
$("#nav dt.nav b").removeClass();
$(this).addClass("clicked");
$("#nav dd:visible").slideUp(200);
$(this).parent().next().slideDown(500);
}
return false;
});
}
$("#nav dd ul a").click(function() {
$.cookie('link_href', $(this).attr('href'));
});
alert($.cookie('link_href'));
var selector = 'a[href="'+ $.cookie('link_href') +'"]';
$(selector).parents('dd').prev().find('b').click();
});
精彩评论