如何在 jQuery 中选择没有给定类的所有元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2448051/
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:32:41  来源:igfitidea点击:

How can I select all elements without a given class in jQuery?

jquery

提问by Andrew G. Johnson

Given the following:

鉴于以下情况:

<ul id="list">
    <li>Item 1</li>
    <li class="active">Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
</ul>

How can I select all but Item 2, AKA something like:

我怎样才能选择除第 2 项之外的所有内容,也就是:

$("ul#list li!active")

回答by Andre Backlund

You can use the .not()method or :not()selector

您可以使用.not()方法或:not()选择器

Code based on your example:

基于您的示例的代码:

$("ul#list li").not(".active") // not method
$("ul#list li:not(.active)")   // not selector

回答by Andy Shellam

What about $("ul#list li:not(.active)")?

怎么样$("ul#list li:not(.active)")

http://api.jquery.com/not-selector/

http://api.jquery.com/not-selector/

回答by Oswaldo Ferreira

You could use this to pick all lielements without class:

您可以使用它来选择li没有类的所有元素:

$('ul#list li:not([class])')

回答by George Sisco

Refer to the jQuery API documentation: not() selectorand not equal selector.

请参阅 jQuery API 文档: not() selectornot equal selector

回答by user3763117

if (!$(row).hasClass("changed")) {
    // do your stuff
}