开发者

Jquery addclass not working when click li

i have menu structure like below

<ul class="menu_bg" id="menu-main-menu">
  <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-653" id="menu-item-653"><a href="http://apptivowp.apptivo.com/" title="Home">Home</a></li>

  <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-650" id="menu-item-650"><a href="http://apptivowp.apptivo.com/artists/" title="Artists">开发者_运维知识库;Artists</a></li>

  <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-651" id="menu-item-651"><a href="http://apptivowp.apptivo.com/category/show-dates" title="Show dates">Show dates</a></li>

  <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-649" id="menu-item-649"><a href="http://apptivowp.apptivo.com/gear/" title="Gear">Gear</a></li>
  <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-563" id="menu-item-563"><a href="http://apptivowp.apptivo.com/category/news" title="News">News</a></li>
</ul>

i used below jquery for to set a class for when clicking the li.

<script type="text/javascript">
jQuery('ul.menu_bg li a').click(function()
{

   jQuery(this)('ul.menu_bg li').addClass('mymenu');
});
</script>

But its not working..the classname mymenu not apllied in for li when i clicked that li.

what is the problem?..

Thanks Ravi


You don't need to re-do the selector, just:

jQuery(this).addClass('mymenu');

will add the class the the a, and since you want to add it to the li, just traverse up the tree first:

$(this).closest('li').addClass('mymenu');


Try:

$(document).ready(function(){
    jQuery('#menu-main-menu li a').click(function() {

       $(this).parent().addClass('mymenu');
    });
});


jQuery(this)('ul.menu_bg li').addClass('mymenu');

The line above is wrong.

use jQuery(this).addClass('mymenu');


As cwolves said you do not need to re-select the item. With jQuery('ul.menu_bg li).click(...) you already have selected the li tag within your list on Click event.

therefore you can directly use then that element: jQuery(this).doWhatYouWant.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜