0

如何在单击链接时在一组元素上循环一个类。这是 HTML 的样子:

<section class="container">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</section>
<a href="#">Iterate Class on Items</a>

JS:

$('a').click(function() {
     $('.container .item').each(function() {
          //cycle .active class on click
     });
});
4

1 回答 1

0
 $('a').click(function() {
     // Activate next
     $('.container .item.active').removeClass('active').next().addClass('active'); 
     // If none are active, activate first
     if ($('.container .item.active').length == 0) 
         $('.container .item').slice(0,1).addClass('active'); 
    });

小提琴:http: //jsfiddle.net/TMene/

于 2012-10-22T03:56:39.237 回答