Detailed explanation of using new methods of html5 to manipulate element class names in JavaScript

Detailed explanation of using new methods of html5 to manipulate element class names in JavaScript

Earlier, JavaScript was not very convenient to process, as it was necessary to first obtain the class attribute and then process the string.

Now html5 adds classList attribute to all elements to manipulate class attributes, which is very convenient.

1. classList attribute

First look at the following code:

<ul class="nav">

    <li class="active">Column 1</li>

    <li>Column 2</li>

    <li>Column 3</li>

</ul>

<script type="text/javascript">

    // Get the current li element let activeLi = document.querySelector(".active");

    // Print classList attribute console.log(activeLi.classList)

    // Console output:

    // DOMTokenList ['active', value: 'active']

</script>

Console view effect:

classList attribute type:

classList attribute is of DOMTokenList type, and the class attributes can be operated through methods such as add , contains , and remove .

  • add(value) adds a new class name.
  • contains(value) Whether it contains the class name.
  • remove(value) deletes the class name.
  • toggle(value) If the class name exists, remove it, otherwise add it.

2. Practical Application

In practice, column switching is a very common application. Continuing with the above example, when clicking a column, move active to the corresponding column

<ul class="nav">

    <li class="item active">Column 1</li>

    <li class="item">Column 2</li>

    <li class="item">Column 3</li>

</ul>

 

<script type="text/javascript">

    let currentElement = document.querySelector(".nav").firstElementChild;

    // Traverse the li elements and set the click event while (currentElement) {

        currentElement.onclick = function() {

            // Remove the active li

            document.querySelector(".active").classList.remove("active");

            // The current li class adds active

            this.classList.add("active");

        }

        currentElement = currentElement.nextElementSibling;

    }

</script>

This makes it very convenient to implement without affecting other class names.

This is the end of this article about the detailed explanation of how to use the new method of html5 to operate element class names in JavaScript . For more relevant content about how to use the new method of html5 to operate element class names in JavaScript , please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript operation element examples
  • Detailed explanation of JavaScript WebAPI, DOM, events and operation element examples
  • js operates two json arrays to merge, remove duplicates, and delete a certain element
  • vue.js click event gets the operation of the current element object
  • JavaScript HTML DOM element (node) add, edit, delete operation example analysis
  • JS document form form element operation complete example
  • Summary of common methods of JavaScript operation elements
  • JavaScript operation elements teach you how to change the page content style

<<:  Detailed explanation of some commonly used font-size font units and line-height in CSS

>>:  The spacing between multiple divs with inline-block is different from the programming method

Recommend

Binary installation of mysql 5.7.23 under CentOS7

The installation information on the Internet is u...

Detailed explanation of nginx's default_server definition and matching rules

The default_server directive of nginx can define ...

Detailed explanation of DIV+CSS naming rules can help achieve SEO optimization

1. CSS file naming conventions Suggestion: Use le...

How to change the tomcat port number in Linux

I have several tomcats here. If I use them at the...

Detailed explanation of the difference between tags and elements in HTML

I believe that many friends who are new to web pag...

How to make a website front end elegant and attractive to users

The temperament of a web front-end website is a fe...

Detailed explanation of Django+Vue+Docker to build an interface testing platform

1. Two words at the beginning Hello everyone, my ...

Use iframe to submit form without refreshing the page

So we introduce an embedding framework to solve th...

Summary of 6 skills needed to master web page production

It has to be said that a web designer is a general...

Pure CSS to add style to select (no script) implementation

Change the default style of select, usually throug...

How to import CSS styles into HTML external style sheets

The link-in style is to put all the styles in one...

Why not use UTF-8 encoding in MySQL?

MySQL UTF-8 encoding MySQL has supported UTF-8 si...