jQuery - 多个 :not 选择器

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

jQuery - multiple :not selector

jqueryjquery-selectors

提问by Hill79

I'm trying to target page-wide links that do not start with a '#' and do not include in-line javascript but I'm having problems figuring out how to structure the selector properly.

我正在尝试定位不以“#”开头且不包含内嵌 javascript 的页面范围链接,但我在弄清楚如何正确构建选择器时遇到问题。

Based on what I've googled about multiple selectors this should work, both selectors work independently, just not together!

根据我在 google 上搜索到的关于多个选择器的内容,这应该可以工作,两个选择器独立工作,而不是一起工作!

$('a:not([href*=javascript]), a:not([href^=#])')
.each(function(){...

回答by jtbandes

Try using

尝试使用

$('a:not([href*=javascript]):not([href^=#])') ...

回答by DarkAjax

You could also try:

你也可以试试:

$('a').not('[href^=#],[href*=javascript]')

回答by Adrien Be

As indicated in jQuery - Multiple Selectors in a :not()?, this is the correct way to do this:

jQuery - 多个选择器中所示:not()? ,这是执行此操作的正确方法:

$( 'a:not([href*=javascript],[href^=#])' )


Don't forget to put quotes around commas if you already already have your selectors to negate in variables

如果您已经有选择器来否定变量,请不要忘记在逗号周围加上引号

var selOne = '[href*=javascript]';
var selTwo = '[href^=#]';
$('a:not(' + selOne + ',' + selTwo + ')')

I admit that the code gets a bit confusing but it has an advantage, you can do things such as this:

我承认代码有点混乱,但它有一个优势,你可以做这样的事情:

var selOne = '[href*=javascript], [href^=#]';
var selTwo = '.anotherSelector, .andAnother, .andSoOn';
$('a:not(' + selOne + ',' + selTwo + ')')

It's useful whenever you need to group selectors for some reason, ie. using the same group of selectors somewhere else in the code.

当您出于某种原因需要对选择器进行分组时,它很有用,即。在代码中的其他地方使用同一组选择器。



A live example using the same technic

使用相同技术的现场示例

$('div:not(.rose-flower,.bus-vehicle)').css('color','red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bus-vehicle">I am a bus</div>
<div class="strawberry-fruit">I am a strawberry</div>
<div class="rose-flower">I am a rose</div>

Also on http://jsfiddle.net/bmL8gz5j/

同样在http://jsfiddle.net/bmL8gz5j/



:notvs .not(): For performance reasons, you should use :notrather than .not(), see Performance differences between using ":not" and ".not()" selectors?

:notvs .not():出于性能原因,您应该使用:not而不是.not(),请参阅使用“:not”和“.not()”选择器之间的性能差异?