jQuery获取没有类属性的所有div [英] jQuery get all divs which do not have class attribute

查看:143
本文介绍了jQuery获取没有类属性的所有div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取所有具有类属性的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])')


修改

jQuery选择器的描述说:

  • [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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆