jQuery selector - all but the first

前端 未结 8 2018
日久生厌
日久生厌 2020-12-07 01:22

I have a small jQuery selectors question, I have the following html:

<
相关标签:
8条回答
  • 2020-12-07 02:03

    @AuthorProxy, @David Thomas and @Maximilian Ehlers all suggest $('.member-info').not('.first').hide(); in their answers which is a very fast, and very readable solution.

    Because of the way jQuery selectors are evaluated right-to-left, the quite readable ".member-info:not(.first)" is actually slowed down by that evaluation.

    A fast and easy to read solution is indeed using the function version .not(".first") or even just .not(":first"):

    e.g.

    $(".member-info").not(".first").hide();   // Class selector
    

    or

    $(".member-info").not(":first").hide();   // Positional selector
    

    JSPerf of related selectors: http://jsperf.com/fastest-way-to-select-all-expect-the-first-one/6

    .not(':first') is only few percentage points slower than slice(1), but is very readable as "I want all except the first one".

    0 讨论(0)
  • 2020-12-07 02:13

    I came across this issue as well. However, I did not conveniently have a class named first marking my element for exclusion. Here was the solution I used for the selector in the context of this example:

    $('.member-info:not(:first)');//grab all .member-info except the first match
    
    0 讨论(0)
提交回复
热议问题