jQuery 选择器 - 除第一个之外的所有选择器
我有一个小的 jQuery 选择器问题,我有以下 html:
<div class="member-info first"></div>
<div class="member-info"></div>
<div class="member-info"></div>
我想隐藏(使用 jQuery)所有包含“member-info”类的 div,但不是包含“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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这使用
not-selector
(docs) 排除first
类的元素。或者,如果
first
类的目的只是识别第一个,则执行以下操作:这使用
slice()
(docs) 方法返回从第二个匹配项开始的集合。This uses the
not-selector
(docs) to exclude elements with thefirst
class.Or if the purpose of the
first
class is simply to identify the first, then do this instead:This uses the
slice()
(docs) method to return a set starting with the second match.使用 :not() 选择器。例如:
如果第一个确实总是第一个孩子,请尝试
Use the :not() selector. For example:
If first is really always the first child, try
我想到的所有可能的方式。
我还制作了 JavaScript性能比较,您可以在其中发现一些意想不到的结果。
所有这些示例都适用于 jQuery v1.10.*。
大多数情况下,这是最快的
$(".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这并不能完全回答您的问题,但您可以使用 gt 跳过第一个匹配的元素。
例如:
请参阅: http://api.jquery.com/gt-selector/
This doesn't quite answer your question, but you could skip the first matched element using gt.
For example:
See: http://api.jquery.com/gt-selector/
我也遇到过这个问题。但是,我没有方便地使用名为
first
的类将我的元素标记为排除。这是我在本示例中用于选择器的解决方案: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:这应该有效:
使用
not()
。This should work:
Using
not()
.怎么样
$('.member-info').not('.first').hide();
how about
$('.member-info').not('.first').hide();
@AuthorProxy
、@David Thomas
和@Maximilian Ehlers
都建议$('.member-info ').not('.first').hide();
在他们的答案中,这是一个非常快速且非常可读的解决方案。由于 jQuery 选择器的计算方式从右到左,相当可读的
".member-info:not(.first)"
实际上因该计算而减慢了速度。一个快速且易于阅读的解决方案确实是使用函数版本
.not(".first")
甚至只是.not(":first")
:例如
或
< strong>JSPerf相关选择器: http://jsperf.com/fastest-way-to-select-all-expect-the-first-one/6
.not(':first')
只有几个百分比点比slice(1)
慢,但非常易读,因为“我想要除第一个之外的所有内容”。@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.
or
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 thanslice(1)
, but is very readable as "I want all except the first one".