jQuery“没有属性”选择? [英] jQuery "Does not have attribute" selector?
问题描述
我可以找到一个具有如下属性的div:
I can find a div that has an attribute like so:
$('.funding-plan-container[data-timestamp]')
但是如果我试图找到一个没有该属性的div,我得到一个错误 - 我的代码是:
But if I try to find a div that does not have that attribute, I get an error - my code is:
$('.funding-plan-container[!data-timestamp]')
jQuery中是否有没有属性选择器?
Is there a "does not have attribute" selector in jQuery?
作为参考,这里的用例是没有动态添加任何没有timestamp属性的div,因此很有用。
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.
谢谢!
推荐答案
使用 :不是()
选择器。
Use the :not()
selector.
$('.funding-plan-container:not([data-timestamp])')
顺便提一下,这是一个有效的 选择器API 选择器,因此它并不特定于jQuery。它将与 querySelectorAll()
一起使用,并在CSS (给定浏览器支持)。
This, by the way, is a valid Selectors API selector, so it isn't specific to jQuery. It'll work with querySelectorAll()
and in your CSS (given browser support).
这篇关于jQuery“没有属性”选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!