How to modify the class attribute in js className

Susan Sarandon
Release: 2023-01-03 09:26:51
Original
8267 people have browsed it

js className Method to modify the class attribute: 1. Modify the class name [p-a-0]; 2. Add another class name [p-a-] while retaining [class="p1"] 0]; 3. The result needs to be deleted [class="addp1"].

How to modify the class attribute in js className

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

js className Method to modify the class attribute:

1. Modify className

How to modify the class attribute in js className

1) Modify the class name to p-a-0

How to modify the class attribute in js className

2) On the basis of retaining class="p1" Add another class named p-a-0

How to modify the class attribute in js className

2 and delete className

How to modify the class attribute in js className1 ).The result needs to be deleted class="addp1"

How to modify the class attribute in js classNameRegular expression analysis:

How to modify the class attribute in js className

3, Check whether a class already exists in the element

if ( document.getElementById("p").className.match(/(?:^|\s)addp1(?!\S)/) ){ console.log("11") }
Copy after login

Related free learning recommendations:

javascript video tutorial

The above is the detailed content of How to modify the class attribute in js className. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!