jQuery - :not() 中的多个选择器?

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

jQuery - Multiple Selectors in a :not()?

jqueryjquery-selectors

提问by Charlie Sheather

I cant seem to get the following working:

我似乎无法得到以下工作:

$('input:not([type=radio][type=checkbox])').live('click', function() {
    alert("You haven't clicked a radio or checkbox!");
});

Have tried a few different syntax's, can anyone help me out on this one.

尝试了几种不同的语法,谁能帮我解决这个问题。

Cheers
Charlie

干杯
查理

回答by Frédéric Hamidi

You're confusing the multiple selectorwith the multiple attribute selector. You should write:

您将多重选择器多重属性选择器混淆了。你应该写:

$("input:not([type=radio], [type=checkbox])");

The multiple attribute selector [type=radio][type=checkbox]matches the elements whose typeattributes are equal to bothradioandcheckbox, which cannot happen in this universe.

多个属性选择器[type=radio][type=checkbox],其中的元素相匹配type的属性是等于两个radiocheckbox,不能在这个宇宙中发生。

回答by darkliquid

The syntax inside the not()is identical to the normal selector syntax, so if you want to match things that are not either of those things, you use a selector in the not that would match both (since essentially you're negating the selector). How do you do that? The same way you apply styles to multiple selectors at once in CSS, with a comma, as so:

里面的语法not()与普通选择器语法相同,所以如果你想匹配不是这些东西中的任何一个的东西,你可以在 not 中使用一个选择器来匹配两者(因为本质上你是在否定选择器)。你是怎样做的?与在 CSS 中一次将样式应用于多个选择器的方式相同,使用逗号,如下所示:

$('input:not([type=radio],[type=checkbox])')

That should select all inputs the not [type=radio]and not [type=checkbox]

那应该选择所有输入 not[type=radio]和 not[type=checkbox]

回答by Junaid Anwar

You can also exclude items like this:

您还可以排除这样的项目:

$('input').not('[type=radio], [type=checkbox]').live('click', function() {
    alert("You haven't clicked a radio or checkbox!");
});