简体   繁体   中英

Using jQuery, how do I select elements that are neither class A nor B?

If I have a list like this:

<ul>
    <li class="A"></li>
    <li class="A B"></li>
    <li class="A C"></li>
</ul>

Using jQuery, how would I select only the first item, but not the other two? In other words, how do I write a selector that will only select an element that does not have class B or C?

$(function() {
    $(".a").not('.b, .c').text('aaa');
});

jsfiddle: http://jsfiddle.net/bitsmix/h2fpq/

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

$('.a').not('.b, .c')

$('.a:not(.b,.c)')

您可以使用.not()方法:

$('li.a').not('.b, .c')

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