开发者

How do I use hasClass to detect if a class is NOT the class I want?

How do I use hasClass so it works as doesNotHaveClass? In other word开发者_运维知识库s, rather than looking to see if it is a specified class, looking to see if it is NOT a specified class. Can I use an exclamation point or something like that?


Yes, you can.

if (!$('element').hasClass('do-not-want')) {
    // This element does not have the .do-not-want class
}


However if you're trying to use selectors to find all the items that don't have a class, perhaps try this:

// find all divs that don't have "badClass" class    
$('div').not('.badClass').each(function(){}); 


if (!$('a').hasClass('xyz')){
  ...
}


Yes, you can As per the jQuery documentation:

The .hasClass() method will return true if the class is assigned to an element, even if other classes also are.

Since the function returns a boolean you can use exclamation point to negate the result.


You can check if the return is false:

if($(element).hasClass("class") === false) {...}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜