跳过 JQuery 中的前 N 个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15213961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Skip first N elements in JQuery
提问by user1224129
I would like to know, how can I skip first N elements in JQuery. Something like this:
我想知道,如何跳过 JQuery 中的前 N 个元素。像这样的东西:
<div id="test">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
...
</div>
$('#test > div').skip(2)
Should return
应该返回
<div>3</div>
<div>4</div>
...
I know I can just use :not(:first-child):not(:first-child + div)...
selector N times, but is there a better way?
我知道我只能使用:not(:first-child):not(:first-child + div)...
选择器 N 次,但有更好的方法吗?
回答by Sani Singh Huttunen
jQuery has a gt selector. (Greater than).
jQuery 有一个gt 选择器。(比...更棒)。
$('#test > div:gt(1)')
Or you can use the slice function
或者你可以使用切片功能
$('#test > div').slice(2)
回答by lordvlad
Use the .slice()function, it gives you the subset of elements based on its index.
使用.slice()函数,它会根据索引为您提供元素的子集。
$('#test > div').slice( 2 )
Reference:http://api.jquery.com/slice/
回答by jokeyrhyme
I think you are looking for the :gt
selector: http://api.jquery.com/gt-selector/Note that you start counting from 0 here.
我认为您正在寻找:gt
选择器:http: //api.jquery.com/gt-selector/请注意,您在这里从 0 开始计数。
Try:
尝试:
$('#test > div:gt(1)')
回答by Baltazar Zaraik
Skip just the first one - example:
跳过第一个 - 例如:
$("#spaccordion li:gt(0)").addClass("collapsed");
All <li>
items will have class "collapsed" except the first one
<li>
除第一个项目外,所有项目都将具有“折叠”类