Home  >  Article  >  Web Front-end  >  How to determine whether a class exists in javascript

How to determine whether a class exists in javascript

WBOY
WBOYOriginal
2021-12-28 18:45:068784browse

Method: 1. Use the getAttribute() method to obtain the attribute value of the specified class; 2. Use the if statement to determine whether the obtained specified class attribute value is empty. The syntax is "if (obtained specified class attribute value=null){No code exists;}else{Code exists;}".

How to determine whether a class exists in javascript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How does javascript determine whether a class exists?

You can use the getAttribute() method to get the value of the attribute by name.

Then use the if statement to determine whether the obtained attribute value is empty.

If it is empty, the specified class does not exist. If it is not empty, the specified class exists.

Examples are as follows:

<div class=&#39;class&#39;></div>
let _class = document.getElementsByTagName(&#39;div&#39;)[0].getAttribute(&#39;class&#39;); 
if (_class == null) {
    console.log(&#39;not class&#39;); 
} else {
    console.log(_class); 
}

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to determine whether a class exists in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn