Javascript jQuery“没有属性”选择器?

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

jQuery "Does not have attribute" selector?

javascriptjquery

提问by H O

I can find a div that has an attribute like so:

我可以找到一个具有如下属性的 div:

$('.funding-plan-container[data-timestamp]')

But if I try to find a div that does not have that attribute, I get an error - my code is:

但是,如果我尝试查找没有该属性的 div,则会出现错误 - 我的代码是:

$('.funding-plan-container[!data-timestamp]')

Is there a "does not have attribute" selector in jQuery?

jQuery 中是否有“没有属性”选择器?

For reference, the use case here is that any div that does not have a timestamp attribute was not added dynamically, and hence is useful.

作为参考,这里的用例是任何没有时间戳属性的 div 都不是动态添加的,因此很有用。

Thanks!

谢谢!

回答by I Hate Lazy

Use the :not()selector.

使用:not()选择器。

$('.funding-plan-container:not([data-timestamp])')


This, by the way, is a valid Selectors APIselector, so it isn't specific to jQuery. It'll work with querySelectorAll()and in your CSS (given browser support).

顺便说一下,这是一个有效的Selectors API选择器,因此它不是特定于 jQuery 的。它将与querySelectorAll()您的 CSS一起使用并在您的 CSS 中使用(提供浏览器支持

回答by Sushanth --

You can filter the selector by using .not()or :not()selector.

您可以使用.not():not()选择器过滤选择器。

$('.funding-plan-container:not([data-timestamp])').

OR

或者

$('.funding-plan-container').not('[data-timestamp]') 

回答by Pez Cuckow

Try it with the :not()pseudo-class selector: http://api.jquery.com/not-selector/

尝试使用:not()伪类选择器:http: //api.jquery.com/not-selector/

$('.funding-plan-container:not([data-timestamp])')