最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove class from element and add to next element - Stack Overflow

programmeradmin2浏览0评论

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?

Ive made a fiddle to hopefully explain my problem, any help would be great, thanks

/

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

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?

Ive made a fiddle to hopefully explain my problem, any help would be great, thanks

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;
});
Share Improve this question asked Dec 19, 2012 at 16:23 LiamLiam 9,85540 gold badges114 silver badges214 bronze badges 2
  • use a variable ? foo = $().find(); foo.remove; foo.next().add, or something (or chain it) – njzk2 Commented Dec 19, 2012 at 16:27
  • 2 By the way, what do you want to happen when you reach the bottom of the list? – Blazemonger Commented Dec 19, 2012 at 16:31
Add a comment  | 

5 Answers 5

Reset to default 10

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. 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. 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');
}

This is actually what you want based on your html structure in you fiddle. http://jsfiddle.net/h6D4k/1/

$('ul.pagination').find('a.active').removeClass('active').parent()
                  .next().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. 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:

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. That's more challenging - and I'm not sure how to do it.

I would write it this way, preventing the action from doing anything on the last li as well.

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

To simplify things, you could attach the active class to the li tags.

Live demo: http://jsfiddle.net/h6D4k/7/

Code:

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

评论列表(0)

  1. 暂无评论