开发者

how to loop through menu and remove class and then add class to current menu item

I have this menu structure that is used to navigate through content slider panels.

 <div id="menu">
  <ul>
    <li><a href="#1" class="cross-link highlight">Bliss Fine Foods</a></li>
    <li><a href="#2" class="cross-link">Menus</a></li>
    <li><a href="#3" class="cross-link">Wines</a></li>  
    <li><a href="#4" class="cross-link">News</a></li>   
    <li><a href="#5" class="cross-link">Contact Us</a></li> 
  </ul>
</div>

I would like to loop through these elements and remove the h开发者_JS百科ighlight class and then add the highlight class to the current / last clicked menu item.

Any ideas? Any help would be greatly appreciated.

Thanks

Jonathan


May be so?

$('#menu li a').click(function(){
  $('#menu li a').removeClass('highlight');
  $(this).addClass('highlight');
});


...your <head> and opening <body> tags...
<div id="menu">
   <ul>
     <li><a href="#1" class="cross-link highlight">Bliss Fine Foods</a></li>
     <li><a href="#2" class="cross-link">Menus</a></li>
     <li><a href="#3" class="cross-link">Wines</a></li>  
     <li><a href="#4" class="cross-link">News</a></li>   
     <li><a href="#5" class="cross-link">Contact Us</a></li> 
  </ul>
</div>
<script type="text/javascript">
(function( $ ) {
 $( "#menu ul li a" ).click( function() {
    $( this ).parent().parent().filter( 'a' ).removeClass( 'highlight' );
    $( this ).addClass( 'highlight' );
 });
})( jQuery );
</script>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜