admin管理员组文章数量:1334325
How do i select everything BUT the element with an id?
<div id="test">
<p id="test"></p>
<p></p>
<p></p>
</div>
I want to be able to select the second and third
How do i select everything BUT the element with an id?
<div id="test">
<p id="test"></p>
<p></p>
<p></p>
</div>
I want to be able to select the second and third
Share Improve this question edited Jul 23, 2010 at 20:44 nicolaskruchten 27.4k8 gold badges88 silver badges103 bronze badges asked Jul 22, 2010 at 14:48 PwnnaPwnna 9,53821 gold badges67 silver badges94 bronze badges 4- 1 You can't have 2 elements with the same id! – nicolaskruchten Commented Jul 22, 2010 at 14:51
- 3 @nik: Yes you can(Obama). It's just not valid and definitly not remendable. – jAndy Commented Jul 22, 2010 at 14:55
- How will jQuery react to having multiple elements with the same ID? :) – nicolaskruchten Commented Jul 22, 2010 at 14:59
- @njk: it will only query/select the first occurence: jsfiddle/v4wkv – jAndy Commented Jul 22, 2010 at 15:08
2 Answers
Reset to default 7You can't have 2 elements with the id "test" but if the code was as follows:
<div id="test">
<p id="test2"></p>
<p></p>
<p></p>
</div>
then you could use
$("#test p").not("#test2")
or
$("#test p:not(#test2)")
to select just the other two paragraphs.
See http://api.jquery./not/ for the documentation for the not() method (first option) or http://api.jquery./not-selector/ for the :not() selector (second option).
Note: this won't select "everything" but rather the second and third paragraph elements. I assume that's what you meant :)
You can bine the :not()
and has-attribute selectors, like this:
$(":not([id])")
A few notes though, you currently have 2 elements with the same ID, this is invalid because IDs should be unique. Also you shouldn't use the selector exactly as I have it above, it should be within something, for example $("#test :not([id])")
to narrow it down ... it's very expensive otherwise.
本文标签: javascriptjQuery selector NOTStack Overflow
版权声明:本文标题:javascript - jQuery selector NOT - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742269403a2444020.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论