jQuery selector - all but the first

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

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

<
相关标签:
8条回答
  • 2020-12-07 01:47
    $('.member-info:not(.first)').hide();
    

    This uses the not-selector(docs) to exclude elements with the first class.

    Or if the purpose of the first class is simply to identify the first, then do this instead:

    $('.member-info').slice(1).hide();
    

    This uses the slice()(docs) method to return a set starting with the second match.

    0 讨论(0)
  • 2020-12-07 01:53

    This should work:

    $('.member-info').not('.first').hide();
    

    Using not().

    0 讨论(0)
  • 2020-12-07 01:55
    $(".member-info:not('.first')").hide();
    $(".member-info").filter(":not('.first')").hide();
    $('.member-info').not('.first').hide();
    
    $(".member-info:not(:first)").hide();
    $(".member-info").filter(":not(':first')").hide();
    $('.member-info').not(':first').hide();
    
    $(".member-info:not(:eq(0))").hide();
    $(".member-info").filter(":not(':eq(0)')").hide();
    $(".member-info").not(":eq(0)").hide();
    
    $(".member-info:not(:lt(1))").hide();
    $(".member-info").filter(":not(':lt(1)')").hide();
    $(".member-info").not(":lt(1)").hide();
    
    $(".member-info:gt(0)").hide();
    $(".member-info").filter(':gt(0)').hide();
    
    $(".member-info").slice(1).hide();
    

    All possible ways that come to my mind. I also made JavaScript performance comparison where you can find some unexpectable results.

    All of this samples work with v1.10.* of jQuery.

    Most times this one is the fastest $(".member-info").slice(1).hide(); and looks like relevant, not brainstormed answer

    0 讨论(0)
  • 2020-12-07 01:55

    Use the :not() selector. For example:

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

    If first is really always the first child, try

    $(".member-info:not(member-info:first)").hide();
    
    0 讨论(0)
  • 2020-12-07 02:01

    This doesn't quite answer your question, but you could skip the first matched element using gt.

    For example:

    $('div.member-info:gt(0)')
    

    See: http://api.jquery.com/gt-selector/

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

    how about $('.member-info').not('.first').hide();

    0 讨论(0)
提交回复
热议问题