Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个元素列表,我想选择大于 2:eq(2) 且小于 6:eq(6) 的孩子。我知道如何选择比使用 :gt() 更大,但我不知道如何在 N 和 M 位置之间进行选择。有一个选择器吗?
你可以随时使用.slice() DOCS
.slice()
jsBin 演示
$('ul li').slice(2,5)
将它们混合在一起:$('...:gt(2):lt(4)')。
$('...:gt(2):lt(4)')
这是:lt(4)因为:lt()“选择索引小于匹配集中索引的所有元素”。所以应该使用6-2。
:lt(4)
:lt()
感谢 Joachim Isaksson 指出:)
另一种方法是:
$('ul li').filter(function(i,elm){ return (i>=2 && i<6);} ).addClass('red');