Javascript 不是 jQuery 中的类选择器

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

Not class selector in jQuery

javascriptjqueryjquery-selectors

提问by Zardoz

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 并尝试

$(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 包含的不仅仅是第一个栏。有没有办法在这样的表达式中使用占位符?类似的东西

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

Any other selectors that may help?

任何其他可能有帮助的选择器?

回答by lonesomeday

You need the :not()selector:

您需要:not()选择器:

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

or, alternatively, the .not()method:

或者,该.not()方法:

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

回答by Sarfraz

You can use the :notfilter selector:

您可以使用:not过滤器选择器:

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

Or not()method:

not()方法:

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

More Info:

更多信息: