jQuery 如何检查元素是否没有特定的类?

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

How to check if an element does NOT have a specific class?

jqueryjquery-selectors

提问by user1005793

How do I check if there isn't a class. For example, I know how to check to see if it has the class "test", but how do I check to see if it doesn't have the class "test"?

我如何检查是否有课程。例如,我知道如何检查它是否有“test”类,但是如何检查它是否没有“test”类?

if($(this).hasClass("test")){
}

回答by sdleihssirhc

if (!$(this).hasClass("test")) {

回答by JJJ

sdleihssirhc's answer is of course the correct one for the case in the question, but just as a reference if you need to select elements that don't have a certain class, you can use the notselector:

sdleihssirhc 的答案当然是问题中案例的正确答案,但作为参考,如果您需要选择没有特定类的元素,您可以使用not选择器:

// select all divs that don't have class test
$( 'div' ).not( ".test" );
$( 'div:not(.test)' );  // <-- alternative 

回答by George-Paul B.

Select element (or group of elements) having class "abc", not having class "xyz":

选择具有类“abc”而不具有类“xyz”的元素(或元素组):

    $('.abc:not(".xyz")')

When selecting regular CSS you can use .abc:not(.xyz).

选择常规 CSS 时,您可以使用.abc:not(.xyz).

回答by Matthew Cira

use the .not() method and check for an attribute:

使用 .not() 方法并检查属性:

$('p').not('[class]');

Check it here: http://jsfiddle.net/AWb79/

在这里检查:http: //jsfiddle.net/AWb79/

回答by aircraft

You can try this:

你可以试试这个:

<div id="div1" class="myClass">there is a class</div>
<div id="div2"> there is no class2 </div>

$(document).ready(function(){
    $("#div2").not('.myClass');  // do not have `myClass` class.
});

回答by Aljo?a

I don't know why, but the accepted answer didn't work for me. Instead this worked:

我不知道为什么,但接受的答案对我不起作用。相反,这有效:

if ($(this).hasClass("test") !== false) {}

回答by omarjebari

There are more complex scenarios where this doesn't work. What if you want to select an element with class A that doesn't contain elements with class B. You end up needing something more like:

还有更复杂的场景,这不起作用。如果你想选择一个不包含类 B 元素的 A 类元素怎么办。你最终需要更多类似的东西:

If parent element does not contain certain child element; jQuery

如果父元素不包含某个子元素;jQuery

回答by le_pragmatist

reading through this 6yrs later and thought I'd also take a hack at it, also in the TIMTOWTDI vein...:D, hoping it isn't incorrect 'JS etiquette'.

6 年后通读这篇文章,并认为我也会对它进行黑客攻击,也是在TIMTOWTDI 静脉中......:D,希望它不是不正确的“JS 礼仪”。

I usually set up a var with the condition and then refer to it later on..i.e;

我通常设置一个带有条件的变量,然后稍后再参考它..ie;

// var set up globally OR locally depending on your requirements
var hC;

function(el) {
  var $this = el;
  hC = $this.hasClass("test");

  // use the variable in the conditional statement
  if (!hC) {
    //
  }
}

Although I should mention that I do this because I mainly use the conditional ternary operator and want clean code. So in this case, all i'd have is this:

虽然我应该提到我这样做是因为我主要使用条件三元运算符并且想要干净的代码。所以在这种情况下,我所拥有的就是:

hC ? '' : foo(x, n) ;
   // OR -----------
!hC ? foo(x, n) : '' ;

...instead of this:

...而不是这个:

$this.hasClass("test") ? '' : foo(x, n) ;
   // OR -----------
(!$this.hasClass("test")) ? foo(x, n) : '' ;