Opposite of jQuery's :eq()

前端 未结 5 1582
悲&欢浪女
悲&欢浪女 2020-12-16 09:14

Does anyone know if there exist some kind of selector to select al the elements from a matched set but the one given by the indicated index. E.g.:

$(\"li\").         


        
相关标签:
5条回答
  • 2020-12-16 10:01

    The other answers will work just fine, but as an alternative you could implement you own custom selector for neq

    $.extend($.expr[":"], {  
        neq: function(elem, i, match) {  
            return i !== (match[3] - 0);
        }  
    });  
    

    And then you could do what you originally suggested.

    $("li:neq(2)").size();
    

    Although another post suggested using .length instead of .size, which will be better as its just a property and not an extra function call.

    $("li:neq(2)").length;
    
    0 讨论(0)
  • 2020-12-16 10:04

    Alright, it's just

    $("li:not(:eq(2))");
    
    0 讨论(0)
  • 2020-12-16 10:06

    I would use filter for such case,

    $('li').filter(function (i, item) {
       return i != 2;
    })
    
    0 讨论(0)
  • 2020-12-16 10:07

    In addition to the custom selector, you could also implement this as a jQuery plugin:

    $.fn.neg = function (index) {
        return this.pushStack( this.not(':eq(' + index + ')') );
    }
    
    0 讨论(0)
  • 2020-12-16 10:12

    Use not:

    $('li').not(':eq(2)');
    
    0 讨论(0)
提交回复
热议问题