jQuery 获取所有没有 class 属性的 div

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

jQuery get all divs which do not have class attribute

jqueryjquery-selectors

提问by Roger

get all divs which has class attribute

获取所有具有 class 属性的 div

$('div[class]')

get all divs which do not have class attribute

获取所有没有 class 属性的 div

$('div[class!=""]')

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

这段代码有效,但我不明白它为什么有效。如果上面的代码有效,那么所有具有 class 属性的 div 的代码应该是

$('div[class=""]') 

which does not yield any result.

这不会产生任何结果。

回答by Gumbo

Try it with the :not()pseudo-class selector:

:not()伪类选择器试试:

$('div:not([class])')


Edit

编辑

The description for the jQuery selectorssay:

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.

This means div[class=""]would select all DIV elements that have a classattribute specified with an empty value.

这意味着div[class=""]将选择具有class指定为空值的属性的所有 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:

但是最后一个选择器是 jQuery 的专有选择器,而不是CSS 选择器。您需要使用:not()来选择所有没有类的 DIV 元素:

div:not([class])

回答by Doug Neiner

What is important to realize is that there are emptyclass attributes as well as elements withouta class attribute, but they require different tests to select.

重要的是要意识到有空的类属性以及没有类属性的元素,但它们需要不同的测试来选择。

There are a number of tests that all do different things. Here is our HTML for our tests:

有许多测试都做不同的事情。这是我们用于测试的 HTML:

<div class="">Empty Class Attribute </div>
<div class="column">Full Class Attribute </div>
<div>No Class Attribute </div>

Now, lets run our tests (The first part is simply a string that helps us know what was just called in the alert, otherwise it is meaningless):

现在,让我们运行我们的测试(第一部分只是一个字符串,帮助我们知道刚刚在警报中调用了什么,否则它毫无意义)

$(document).ready(function(e){
  // Outputs "Empty Class Attribute Full Class Attribute"
  alert( "div[class] : "     + $('div[class]').text()     );

  // Outputs "Full Class Attribute"
  alert( "div[class!=''] : " + $('div[class!=""]').text() );

  // Outputs "Empty Class Attribute" 
  alert( "div[class=''] : "  + $('div[class=""]').text()  );

  // Outputs "No class Attribute"
  alert( "div:not([class]) : " + $('div:not([class])').text()     );
});

You can view this code in your browser by visiting here: http://jsbin.com/ijupu

您可以通过访问此处在浏览器中查看此代码:http: //jsbin.com/ijupu

Now, armed with this knowledge, if you wanted to select every divelement on the page with either a blank attribute and no attribute, use the following selector:

现在,有了这些知识,如果您想选择div页面上具有空白属性和无属性的每个元素,请使用以下选择器:

$("div[class=''], div:not([class])");

回答by Atli

The $('div[class=""]')selector essentially reads: "Get all div elements whose class attribute has an empty string as its value."- That excludes all div elements that have ANY value in the class attribute, other than an empty string, and all div elements that do not have the class attribute set at all.

$('div[class=""]')选择基本上写着:“把所有的div元素,它的类属性有一个空字符串作为它的价值。” - 这不包括在 class 属性中具有任何值的所有 div 元素,除了空字符串,以及所有根本没有设置 class 属性的 div 元素。

回答by shadrachJabonir

try

尝试

jQuery('div[class^=""]')

or

或者

$('div[class^=""]')

it means get all div which has class with any names

这意味着获取所有具有任何名称的类的 div