Home  >  Article  >  Web Front-end  >  How to modify the class attribute in js className

How to modify the class attribute in js className

coldplay.xixi
coldplay.xixiOriginal
2021-03-02 14:52:218282browse

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")
}

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!

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