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

后端 未结 8 1302
长发绾君心
长发绾君心 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:30

    It's the .className property, like this:

    if (document.getElementById('element').className == "class_one") {
        //code...
    }
    

提交回复
热议问题