Check if an element contains a class in JavaScript?

后端 未结 27 2770
面向向阳花
面向向阳花 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:10

    I know there a lot of answers but most of these are for additional functions and additional classes. This is the one I personally use; much cleaner and much less lines of code!

    if( document.body.className.match('category-page') ) { 
      console.log('yes');
    }
    

提交回复
热议问题