简体   繁体   English

从元素中删除类并添加到下一个元素

[英]Remove class from element and add to next element

I have a list of links, one has the class active. 我有一个链接列表,一个类活跃。

On my next button click id like to remove the class from the current element and add it to the next only I cant seem to get it to add? 在我的下一个按钮上单击id就像从当前元素中删除该类并将其添加到下一个我似乎无法添加它?

Ive made a fiddle to hopefully explain my problem, any help would be great, thanks 我做了一个小提琴,希望能解释我的问题,任何帮助都会很棒,谢谢

http://jsfiddle.net/h6D4k/ http://jsfiddle.net/h6D4k/

$('.next').click(function(){
    $('ul.pagination').find('a.active').removeClass('active');
    $('ul.pagination').find('a.active').next('a').addClass('active');
    return false;
});

One of the jQuery most usable conveniencies is that its methods are (usually) chainable - in other words, they return the very object they are called from. jQuery最常用的一个方面是它的方法(通常)是可链接的 - 换句话说,它们返回它们被调用的对象。 So you can simply write this: 所以你可以简单地写一下:

$('ul.pagination').find('a.active').removeClass('active').closest('li')
                  .next('li').find('a').addClass('active');

... as it's <li> elements that should be 'nexted', not <a> ones. ......因为它的<li>元素应该是'nexted',而不是<a>的元素。 But in fact, you shouldn't probably discard 'active' altogether if it's the last element in question: 但事实上,如果它是最后一个有问题的元素,你不应该完全放弃'主动':

var $a      = $('ul.pagination').find('a.active'),
    $li     = $a.closest('li'),
    $nextLi = $li.next('li');

if ($nextLi.length) {
   $a.removeClass('active');
   $nextLi.find('a').addClass('active');
}

Because once you've done this... 因为一旦你完成了这个......

$('ul.pagination').find('a.active').removeClass('active');

There is no more a.active - the active classname has been removed from that element. 没有更多的a.active - 已从该元素中删除active类名。 So repeating the same selector... 所以重复相同的选择器......

$('ul.pagination').find('a.active')//...

... will select nothing. ......什么都不会选。

Chain it all together instead. 把它们连在一起代替。

$('ul.pagination').find('a.active').removeClass('active').next('a').addClass('active');

You have a second problem. 你有第二个问题。 According to the jQuery API for next() , it will: 根据jQuery API for next() ,它将:

Get the immediately following sibling of each element in the set of matched elements. 获取匹配元素集中每个元素的紧随其后的兄弟。 If a selector is provided, it retrieves the next sibling only if it matches that selector. 如果提供了选择器,则仅当它与该选择器匹配时,它才会检索下一个兄弟。

You're not trying to get the following sibling: 你不是想要得到以下兄弟:

<ul class="pagination">
    <li><a class="one active" href="#">X</a></li>
    <li><a class="two" href="#">X</a></li>
    <li><a class="three" href="#">X</a></li>
</ul>

<a href="" class="next">Next</a>
<a href="" class="prev">Prev</a>

You're trying to get the next <a> in the whole document. 您正试图在整个文档中获取下一个<a> That's more challenging - and I'm not sure how to do it. 这更具挑战性 - 我不知道该怎么做。

This is actually what you want based on your html structure in you fiddle. 这实际上是你想要的基于你的html结构的小提琴。 http://jsfiddle.net/h6D4k/1/ http://jsfiddle.net/h6D4k/1/

$('ul.pagination').find('a.active').removeClass('active').parent()
                  .next().find('a').addClass('active');

I would write it this way, preventing the action from doing anything on the last li as well. 我会用这种方式写它,防止动作在最后一个li上做任何事情。

http://jsfiddle.net/h6D4k/6/ http://jsfiddle.net/h6D4k/6/

$('.next').click(function(e){
    e.preventDefault();
    if ($("ul.pagination a.active").parent().is(":last-child")) return;
    $('ul.pagination a.active').removeClass('active').parent().next().find("a").addClass('active');
});

You have two errors in your code: 您的代码中有两个错误:

  1. Once removed, the active class can't be found anymore 删除后,无法再找到活动类
  2. your a tags are nested in li tags so next() doesn't work as you expect 你的标签嵌套在li标签中,所以next()不能按预期工作

To simplify things, you could attach the active class to the li tags. 为了简化操作,您可以将活动类附加到li标记。

Live demo: http://jsfiddle.net/h6D4k/7/ 现场演示: http//jsfiddle.net/h6D4k/7/

Code: 码:

$('.next').click(function(){
  $('ul.pagination').find('li.active').removeClass('active')
                    .next().addClass('active');
  return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM