Is there any jquery selector to support round robin?

风流意气都作罢 提交于 2019-12-25 02:55:21

问题


Is there an easy way to select next() child in JQuery so that if current is last then next should be the first sibling?


回答1:


You can use eq on parent element.

var len = $('#mySelector .child').length;
$('#mySelector .child').eq(i % len)....

HTML

<div id="mySelector">
    <span class="child"></span>
    <span class="child"></span>
    <span class="child"></span>
    <span class="child"></span>
    <span class="child"></span>
</div>

Demo: http://jsfiddle.net/tusharj/88n9w98e/




回答2:


Short answer, no. However, you can check if next() returns anything and if not retrieve the first sibling. Something like this:

var $next = $el.next()
if ($next.length == 0)
    $next = $el.siblings().first();

Example fiddle




回答3:


Simply put:

// if current is the current DOMElement
next = current.nextSibling || current.parentNode.firstChild;

No jQuery needed, these are all DOM features which execute faster than any library implementation.



来源:https://stackoverflow.com/questions/30621218/is-there-any-jquery-selector-to-support-round-robin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!