问题
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