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

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

提问人:Andrew G. Johnson 提问时间:3/15/2010 更新时间:9/13/2016 访问量:181615

问:

鉴于以下几点:

<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>

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

$("ul#list li!active")
jQuery查询

评论

26赞 N 1.1 3/15/2010
$("ul#list").not(".active")$("ul#list:not(.active)")

答:

478赞 Andre Backlund 3/15/2010 #1

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

基于示例的代码:

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

评论

16赞 Nishantha 8/5/2015
如果要检查两个类,请使用.not(".completed, .current")
0赞 Ivan Kolyhalov 10/26/2019
在 2019 年vanillaJs: document.querySelectorAll('.foo-class:not(.bar-class):not(.foobar-class'))
6赞 George Sisco 3/15/2010 #2

请参阅 jQuery API 文档:not() 选择器和不相等选择器

43赞 Andy Shellam 3/15/2010 #3

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

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

21赞 Oswaldo Ferreira 12/10/2013 #4

你可以用它来挑选所有没有类的 li 元素:

$('ul#list li:not([class])')
2赞 user3763117 5/27/2015 #5
if (!$(row).hasClass("changed")) {
    // do your stuff
}