Keeping menu extension open on page change
I have an expanding menu using jquery. It works well except for one thing: when the user navigates away from the page the menu collapses. I want the menu to stay open when the user clicks on any of the final < li > links. All of these links open the same page. Content on these pages is grabbed from a database and determined by the ?id=.
Here's the code:
HTML:
<ul id="nav">
<li><a href="javascript:;">Products</a>
<ul id="nav2">
<li><a href="javascript:;">Domestic Market</a>
<ul>
<li><a href="index.php?page=Product&id=1">Link1</a></li>
<li><a href="index.php?page=Product&id=2">Link2</a></li>
<li><a href="index.php?page=Product&id=3">Link3</a></li>
</ul>
</li>
<li><a href="javascript:;">Commercial Market</a>
<ul>
<li><a href="">Coming Soon</a></li>
</ul>
开发者_开发问答 </li>
</ul>
</li>
</ul>
CSS:
ul {
padding: 0px;
margin: 0px;
width: 10em;
list-style-type:none;
}
li {
font: 100% Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
color:#003;
}
li ul {
display: none;
}
li.active > ul {
display: block;
}
jQuery:
$('#nav').delegate('li,a', 'click', function(e) {
e.stopPropagation();
var self = $(e.target),
//get a reference to the clicked element
active = self.parents().andSelf() //select all li's that should be active
.addClass('active'); //and activate them
$('#nav .active').not(active).removeClass('active'); //deactivate others
});
$(document).click(function(){
$('.active').removeClass('active');
});
How do I keep the final < li >'s open?
I would recommend storing the value in a cookie and then setting it on page load.
First off, I'd recommen you download the jQuery cookie plugin here. It's a good plugin and it's small. Set the value:
$.cookie("cookie_name", "cookie_value");
And then on document.ready you can pull the value:
$.cookie("cookie_name);
and set it appropriately with something resembling your current click function.
精彩评论