How check if body has a specific class with JavaScript?

后端 未结 7 1927
不思量自难忘°
不思量自难忘° 2021-01-01 16:40

How can I check if body has specific class? This is my case:


7条回答
  •  情书的邮戳
    2021-01-01 17:19

    function hasClass(ele,cls) {
         return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
    }
    
    if(hasClass(document.getElementById("test"), "test")){//do something};
    

    maybe this helps you :-)

    With the use of jQuery it would be easier and less code but nevermind !

提交回复
热议问题