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

How to determine whether a class exists in javascript

WBOY
WBOY Original
2021-12-28 18:45:06 8763browse

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:

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!

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