Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery:选择给定类的所有元素,特定Id除外_Jquery_Jquery Selectors - Fatal编程技术网

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

jQuery:选择给定类的所有元素,特定Id除外,jquery,jquery-selectors,Jquery,Jquery Selectors,这可能很简单 我想选择给定类thisClass的所有元素,id为thisId的除外 i、 e.等同于-/减意味着删除: $(".thisClass"-"#thisId").doAction(); 使用 如果您有多个ID或选择器,则只需使用逗号分隔符,此外: .thisclass:notthisid,thatid.doAction $.thisClass[id!='thisId'].doAction 选择器文档:或采用.not方法 使用.not方法选择整个元素也是一个选项 如果您想直接对该元素执

这可能很简单

我想选择给定类thisClass的所有元素,id为thisId的除外

i、 e.等同于-/减意味着删除:

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

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

.thisclass:notthisid,thatid.doAction

$.thisClass[id!='thisId'].doAction


选择器文档:

或采用.not方法

使用.not方法选择整个元素也是一个选项

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

$.thisClass.not$thisId[0].doAnotherAction.doAction; 您可以使用如下示例中的.not函数删除具有确切id、包含特定单词的id、以单词开头的id等项。。。有关jQuery选择器的更多信息,请参阅

按确切ID忽略:

 $(".thisClass").not('[id="thisId"]').doAction();
忽略包含单词ID的ID

$(".thisClass").not('[id*="Id"]').doAction();
忽略ID是从我的

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

我会给出一个JS ES6答案,以防有人在寻找它:

Array.from(document.querySelectorAll(".myClass:not(#myId)")).forEach((el,i) => {
    doSomething(el);
}
更新在我发布原始答案时,这可能是可能的,但现在仍然添加了以下内容:

document.querySelectorAll(".myClass:not(#myId)").forEach((el,i) => {
    doSomething(el);
});
这将从使用中删除Array.com

document.queryselectoral返回一个节点列表。
阅读此处,了解更多有关如何迭代它和其他内容的信息:

您是指$.thisClass[id!='thisId'].doAction?如果要排除多个类,该怎么办?文档中的thx:所有选择器都在:not内接受,例如::notdiv a和:notdiv,a,因此只需使用逗号分隔的选择器即可执行多个操作;使用这样的单引号-$.thisclass:不是'thisid'。doAction;或者,如果您希望绑定到具有特定类名的元素的所有子元素(只有一个除外),您可以这样做:$'.thisclass:notid.otherclass.DoAction如果我有一个标准代码,如:$'some'。不重要,$'Other.dosomethingelse和我希望避免在动态给定的特定id上执行。not不是选择器。这是一个函数。但也有:不像其他答案提到的选择器。
Array.from(document.querySelectorAll(".myClass:not(#myId)")).forEach((el,i) => {
    doSomething(el);
}
document.querySelectorAll(".myClass:not(#myId)").forEach((el,i) => {
    doSomething(el);
});