jQuery获取没有类属性的所有div [英] jQuery get all divs which do not have class attribute
问题描述
获取所有具有类属性的div
get all divs which has class attribute
$('div[class]')
获取所有没有class属性的div
get all divs which do not have class attribute
$('div[class!=""]')
此代码有效,但我不明白为什么有效.如果上述代码有效,则所有具有class属性的div的代码应为
This code works but I don't understand why it works. If the above code works then the code for all divs with class attribute should be
$('div[class=""]')
不会产生任何结果.
推荐答案
尝试使用 :not()
伪类选择器:
Try it with the :not()
pseudo-class selector:
$('div:not([class])')
修改
-
[attribute]
匹配具有指定属性的元素. -
[attribute=value]
将具有指定属性的元素与特定值匹配. -
[attribute!=value]
匹配不具有指定属性或具有指定属性但不具有特定值的元素.
[attribute]
Matches elements that have the specified attribute.[attribute=value]
Matches elements that have the specified attribute with a certain value.[attribute!=value]
Matches elements that either don't have the specified attribute or do have the specified attribute but not with a certain value.
这意味着div[class=""]
将选择所有具有class
属性并指定为空值的DIV元素.
This means div[class=""]
would select all DIV elements that have a class
attribute specified with an empty value.
但是最后一个选择器是jQuery的专有选择器,而不是 CSS选择器 .您需要使用:not()
选择所有没有类的DIV元素:
But the last selector is a proprietary selector of jQuery and not a CSS selector. You would need to use :not()
to select all DIV elements that do not have a class:
div:not([class])
这篇关于jQuery获取没有类属性的所有div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!