Check if an element contains a class in JavaScript?

后端 未结 27 2222
面向向阳花
面向向阳花 2020-11-22 09:36

Using plain JavaScript (not jQuery), Is there any way to check if an element contains a class?

Currently, I\'m doing this:

27条回答
  •  有刺的猬
    2020-11-22 10:08

    If the element only has one class name you can quickly check it by getting the class attribute. The other answers are much more robust but this certainly has it's use cases.

    if ( element.getAttribute('class') === 'classname' ) {
    
    }
    

提交回复
热议问题