jQuery:选择给定类的所有元素,特定 Id 除外

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2551217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:47:03  来源:igfitidea点击:

jQuery: select all elements of a given class, except for a particular Id

jqueryjquery-selectors

提问by Ankur

This is probably pretty simple.

这可能很简单。

I want to select all elements of a given class thisClass, except where the id is thisId.

我想选择给定类的所有元素thisClass,除了 id 所在的位置thisId

i.e. something equivalent to (where -/minus implies remove):

即相当于(其中 -/minus 表示删除):

$(".thisClass"-"#thisId").doAction();

回答by rahul

Use the :not selector.

使用:not 选择器

$(".thisclass:not(#thisid)").doAction();

If you have multiple ids or selectors just use the comma delimiter, in addition:

如果您有多个 id 或选择器,只需使用逗号分隔符,另外:

(".thisclass:not(#thisid,#thatid)").doAction();

(".thisclass:not(#thisid,#thatid)").doAction();

回答by Karl Adler

Or take the .not() method

或者采用 .not() 方法

https://api.jquery.com/not/

https://api.jquery.com/not/

$(".thisClass").not("#thisId").doAction();

回答by ScottyG

You could use the .notfunction like the following examples to remove items that have an exact id, id containing a specific word, id starting with a word, etc... see http://www.w3schools.com/jquery/jquery_ref_selectors.aspfor more information on jQuery selectors.

您可以像以下示例一样使用.not函数来删除具有确切 id、包含特定单词的 id、以单词开头的 id 等的项目...请参阅http://www.w3schools.com/jquery/jquery_ref_selectors .asp了解更多关于 jQuery 选择器的信息。

Ignore by Exact ID:

按确切 ID 忽略:

 $(".thisClass").not('[id="thisId"]').doAction();

Ignore ID's that contains the word "Id"

忽略包含“Id”一词的 ID

$(".thisClass").not('[id*="Id"]').doAction();

Ignore ID's that start with "my"

忽略以“my”开头的 ID

$(".thisClass").not('[id^="my"]').doAction();

回答by Jayant Bhawal

I'll just throw in a JS (ES6) answer, in case someone is looking for it:

我只是抛出一个 JS (ES6) 答案,以防有人在寻找它:

Array.from(document.querySelectorAll(".myClass:not(#myId)")).forEach((el,i) => {
    doSomething(el);
}

Update (this may have been possible when I posted the original answer, but adding this now anyway):

更新(当我发布原始答案时,这可能是可能的,但无论如何现在添加这个):

document.querySelectorAll(".myClass:not(#myId)").forEach((el,i) => {
    doSomething(el);
});

This gets rid of the Array.fromusage.

这摆脱了Array.from使用。

document.querySelectorAllreturns a NodeList.
Read here to know more about how to iterate on it (and other things): https://developer.mozilla.org/en-US/docs/Web/API/NodeList

document.querySelectorAll返回一个NodeList.
在此处阅读以了解有关如何对其进行迭代(以及其他内容)的更多信息:https: //developer.mozilla.org/en-US/docs/Web/API/NodeList

回答by Amy B

$(".thisClass[id!='thisId']").doAction();

$(".thisClass[id!='thisId']").doAction();

Documentation on selectors: http://api.jquery.com/category/selectors/

关于选择器的文档:http: //api.jquery.com/category/selectors/

回答by NiZa

Using the .not()method with selecting an entire element is also an option.

使用.not()选择整个元素的方法也是一种选择。

This way could be usefull if you want to do another action with that element directly.

如果您想直接对该元素执行另一个操作,这种方式可能很有用。

$(".thisClass").not($("#thisId")[0].doAnotherAction()).doAction();