admin管理员组文章数量:1134569
I can select (using jQuery) all the divs in a HTML markup as follows:
$('div')
But I want to exclude a particular div
(say having id="myid"
) from the above selection.
How can I do this using Jquery functions?
I can select (using jQuery) all the divs in a HTML markup as follows:
$('div')
But I want to exclude a particular div
(say having id="myid"
) from the above selection.
How can I do this using Jquery functions?
Share Improve this question edited Sep 15, 2017 at 11:22 Om Sao 7,6232 gold badges48 silver badges68 bronze badges asked Oct 29, 2011 at 10:21 siva636siva636 16.4k25 gold badges101 silver badges135 bronze badges 1- 2 use the not selector in jquery – abhijit Commented Oct 29, 2011 at 10:23
7 Answers
Reset to default 188Simple:
$('div').not('#myid');
Using .not()
will remove elements matched by the selector given to it from the set returned by $('div')
.
You can also use the :not()
selector:
$('div:not(#myid)');
Both selectors do the same thing, however :not()
is faster, presumably because jQuery's selector engine Sizzle can optimise it into a native .querySelectorAll()
call.
var els = toArray(document.getElementsByTagName("div"));
els.splice(els.indexOf(document.getElementById("someId"), 1);
You could just do it the old fashioned way. No need for jQuery with something so simple.
Pro tips:
A set of dom elements is just an array, so use your favourite toArray
method on a NodeList
.
Adding elements to a set is just
set.push.apply(set, arrOfElements);
Removing an element from a set is
set.splice(set.indexOf(el), 1)
You can't easily remove multiple elements at once :(
$("div:not(#myid)")
[doc]
or
$("div").not("#myid")
[doc]
are main ways to select all but one id
You can see demo here
var elements = $('div').not('#myid');
This will include all the divs except the one with id 'myid'
$('div:not(#myid)');
this is what you need i think.
That should do it:
$('div:not("#myid")')
You use the .not
property of the jQuery library:
$('div').not('#myDiv').css('background-color', '#000000');
See it in action here. The div #myDiv will be white.
本文标签: javascriptquotAll but notquot jQuery selectorStack Overflow
版权声明:本文标题:javascript - "All but not" jQuery selector - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736840702a1955087.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论