简体   繁体   English

如何使用jQuery在兄弟姐妹之间循环?

[英]How to cycle through siblings using jQuery?

I have the folowing code:我有以下代码:

html: html:

<div class="container">
    <div class="selected">A</div>
    <div>B</div>
    <div>C</div>
    <div>D</div>
</div>
<button id="next">next!</button>

jQuery: jQuery:

$("#next").click(function() {
    $(".selected").removeClass("selected").next().addClass("selected");
});

What i want is loop through the divs in the container.我想要的是循环遍历容器中的 div。 I can do this to cycle:我可以这样做来循环:

$("#next").click(function() {
    if ($(".selected").next().length == 0) {
        $(".selected").removeClass("selected").siblings(":nth-child(1)").addClass("selected");
    }
    else {
        $(".selected").removeClass("selected").next().addClass("selected");
    }
});

But i think there is a simpler way.但我认为有一个更简单的方法。 How can i make it simpler ?我怎样才能让它更简单? (I don't mind if you don't use the next() function). (我不介意你不使用next()函数)。

jsFiddle: http://jsfiddle.net/S28uC/ jsFiddle: http : //jsfiddle.net/S28uC/

I 'd prefer siblings.first() instead of siblings(":nth-child(1)") , but in essence you won't be able to wrap around without using some variant of next().length .我更喜欢siblings.first()而不是siblings(":nth-child(1)") ,但本质上,如果不使用next().length一些变体,你将无法环绕。

Update: If I were writing this from scratch, this is how I 'd do it:更新:如果我从头开始写这个,我会这样做:

$("#next").click(function() {
    var $selected = $(".selected").removeClass("selected");
    var divs = $selected.parent().children();
    divs.eq((divs.index($selected) + 1) % divs.length).addClass("selected");
});

This approach is motivated by two factors:这种方法受到两个因素的推动:

  1. When you want to cycle over a collection indefinitely, modulo comes to mind当你想无限循环遍历一个集合时,你会想到模数
  2. Getting rid of the if makes for smarter-looking code去掉if使代码看起来更智能

When setting the value of divs I preferred $selected.parent().children() over the equivalent $selected.siblings().add($selected) as a matter of taste -- there are practically endless possibilities.在设置divs的值时,我更喜欢$selected.parent().children()而不是等效的$selected.siblings().add($selected)作为品味问题——实际上有无限的可能性。

how about this.这个怎么样。

...
var selected = $(".selected").removeClass("selected");
if (jQuery(selected).next().addClass("selected").length == 0
   {jQuery(selected).siblings().first().addClass("selected");};
...

In old good AI manner you try to do the deed (addClass), if it worked (length <> 0) nothing more to do, otherwise you try again on the first of the siblings.在旧的良好 AI 方式中,您尝试执行操作 (addClass),如果它有效 (length <> 0),则无需再执行任何操作,否则您将在第一个兄弟姐妹上再次尝试。

You can try this你可以试试这个

var  cont   = $('.container'),
     i      = 0;
$("#next").on('click', function() {
    cont.children().removeClass('selected');
    i += 1;
    if ( i === document.querySelectorAll('.container div').length ) { i = 0; }
    cont.children().eq(i).addClass('selected');
});

 var cont = $('.container'), i = 0; $("#next").on('click', function() { cont.children().removeClass('selected'); // increase index for each click i += 1; // reset i if it reached to last index //(hack to force next to go back to first element when we are at the end) if ( i === document.querySelectorAll('.container div').length ) { i = 0; } cont.children().eq(i).addClass('selected'); });
 .selected { background-color: yellow; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="selected">A</div> <div>B</div> <div>C</div> <div>D</div> </div> <button id="next">next!</button>

simply you will increase i for each click and when it reach the end ( div s length ) it will be reset.只需每次点击都会增加i ,当它到达末尾( div s 长度)时,它将被重置。

一种简单的方法是:

$("#container").find("div:eq(0)").addClass("selected");

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

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