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;}".
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='class'></div> let _class = document.getElementsByTagName('div')[0].getAttribute('class'); if (_class == null) { console.log('not class'); } 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!