开发者

Remove class with jquery from LI

开发者 https://www.devze.com 2023-01-05 02:14 出处:网络
This adds the class to a li.When another li is clicked it also adds the class current. But now i have two current.You\'ll开发者_JAVA百科 notice below im trying to remove the class current from the li

This adds the class to a li. When another li is clicked it also adds the class current. But now i have two current. You'll开发者_JAVA百科 notice below im trying to remove the class current from the li which was initially current.

var
$tabs = $('.promo-tabs li'),
$panes = $('.promo-panes > div');

$tabs.click(function(){
  $(this).addClass('current')
  $(this).parent().siblings().find('li').removeClass('current');
  $panes.eq($tabs.index(this)).fadeIn().siblings().hide();
  return false;
});

 //Init Promo Tabs
$tabs.first().click();

Any ideas on how to tweak this line so the class is removed once another li is clicked would be great

  $(this).parent().siblings().find('li').removeClass('current');


Why not just

$tabs.removeClass('current')

placed before

$(this).addClass('current')

? In other words, just get rid of "current" from all the <li> elements, and then add it to the one that's been clicked.


The following should work:

$tabs.click(function(){
  $tabs.removeClass('current');      
  $(this).addClass('current')
  $panes.eq($tabs.index(this)).fadeIn().siblings().hide();
  return false;
});


I think the answer to your question is that this

$(this).parent().siblings().find('li').removeClass('current');

Checks for li tags under the siblings of the parent. Either of these should work

$(this).siblings().removeClass('current');
$(this).parent().find('li').removeClass('current');

But I think the most efficient solution would be

$(this).parent().find('li.current').removeClass('current');
$(this).addClass('current');
0

精彩评论

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