admin管理员组

文章数量:1128487

Is there a simple selector expression to not select elements with a specific class?

<div class="first-foo" />
<div class="first-moo" />
<div class="first-koo" />
<div class="first-bar second-foo" />

I just want to get the first three divs and tried

$(div[class^="first-"][class!="first-bar"])

But this receives all as the last div contains more than first-bar. Is there a way to use a placeholder in such an expression? Something like that

$(div[class^="first-"][class!="first-bar*"]) // doesn't seem to work

Any other selectors that may help?

Is there a simple selector expression to not select elements with a specific class?

<div class="first-foo" />
<div class="first-moo" />
<div class="first-koo" />
<div class="first-bar second-foo" />

I just want to get the first three divs and tried

$(div[class^="first-"][class!="first-bar"])

But this receives all as the last div contains more than first-bar. Is there a way to use a placeholder in such an expression? Something like that

$(div[class^="first-"][class!="first-bar*"]) // doesn't seem to work

Any other selectors that may help?

Share Improve this question asked Jan 6, 2011 at 10:51 medihackmedihack 16.6k21 gold badges91 silver badges140 bronze badges 2
  • Scratch my earlier comment, I just reread the question. Critical class is first-bar. – BoltClock Commented Jan 6, 2011 at 10:54
  • 1 In case one wants to select all elements that do not have either class1 or class2, concatenating would work: $('div[class^="first-"]').not('.class1').not('.class2') – J0ANMM Commented Nov 15, 2017 at 12:06
Add a comment  | 

4 Answers 4

Reset to default 675

You need the :not() selector:

$('div[class^="first-"]:not(.first-bar)')

or, alternatively, the .not() method:

$('div[class^="first-"]').not('.first-bar');

You can use the :not filter selector:

$('foo:not(".someClass")')

Or not() method:

$('foo').not(".someClass")

More Info:

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

You can write a jQuery selector in the "not" method:

$('div[class^="first-"]').not($('.first-bar'))

It also works on jQuery events with :not() selector.

Simply like this :

jQuery(document).on('click', '.fo-line:not(.deleted) .clickable', function(e) {
    e.preventDefault();
    // do your stuff...
});

In my case, I enable click on every <td class="clickable"> of <tr class="fo-line"> expected all with deleted class (<tr class="fo-line deleted">)

本文标签: javascriptNot class selector in jQueryStack Overflow