简体   繁体   中英

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

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:

$("ul#list li!active")

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

Code based on your example:

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

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

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

您可以使用它来选择所有li元素而不使用class:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM