开发者

jQuery selector - all but the first

开发者 https://www.devze.com 2023-02-07 02:28 出处:网络
I have a small jQuery selectors question, I have the following html: <div class=\"member-info first\"></div>

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

<div class="member-info first"></div>
<div class="member-info"></div>
<div class="member-info"></div>

I开发者_开发百科 want to hide (using jQuery) all the divs that holds the "member-info" class, but not the one holding the "first" class, any thoughts?


$('.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.


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();


$(".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


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/


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


This should work:

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

Using not().


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


@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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号