Opposite of jQuery's :eq()
Opposite of jQuery's :eq()
有人知道是否存在某种选择器来从匹配集中选择所有元素吗。例如:
$("li").neq(2).size();
假设有5个元素,最后一条语句将给出4个元素,并且将包含DOM中除第二个元素之外的所有<li>
元素。
使用not
:
$('li').not(':eq(2)');
好吧,这只是
$("li:not(:eq(2))");
其他答案也可以,但作为替代方案,您可以为neq
实现自己的自定义选择器
$.extend($.expr[":"], {
neq: function(elem, i, match) {
return i !== (match[3] - 0);
}
});
然后你可以按照你最初的建议去做。
$("li:neq(2)").size();
尽管另一篇文章建议使用.length
而不是.size
,这将更好,因为它只是一个属性,而不是一个额外的函数调用。
$("li:neq(2)").length;
对于这种情况,我会使用过滤器,
$('li').filter(function (i, item) {
return i != 2;
})
除了自定义选择器,您还可以将其实现为jQuery插件:
$.fn.neg = function (index) {
return this.pushStack( this.not(':eq(' + index + ')') );
}
相关文章:
- HighChart : Usage of setData
- Difference between methods of defining JavaScript 'class
- jQuery eq()导致无限迭代
- 为什么我在下面的..of循环中得到意外令牌
- Appending the innerhtml of <th>
- Animation cuts off multiple lines of text in <p>
- javascript: functionname() instread of functionname()
- jQuery使用.of()删除事件处理程序时出现的问题
- Three.js issue creating meshes outside of loader's load(
- jquery in the middle of html
- JavaScript equivalent of C#'s DynamicObject?
- nodejs console.log of object
- value of jquery sortable
- Javascript imageData returns array of 0's
- 对于grunt构建中的(let.of)错误
- Opposite of jQuery's :eq()
- Implementation of jQuery 'animate' with Meteor
- 当调用.of()然后调用.on()时,Jquery是否可能将事件推迟
- Pure JS equivalent of Jquery eq()
- Css equivalent of :eq