How can I check in JavaScript if a DOM element contains a class?

后端 未结 8 1304
长发绾君心
长发绾君心 2020-12-14 08:57

How can I check in JavaScript if a DOM element contains a class?

I tried the following code, but for some reason it doesn\'t work...

if (document.get         


        
8条回答
  •  一生所求
    2020-12-14 09:21

    If you are using jQuery then just this simple code will help:

    if ($('.yourclass').length) {
      // do something
    } 
    

    If you like to check more than 2 classes in the page then use $('.yourclass').length > 2

提交回复
热议问题