Earlier, JavaScript was not very convenient to process, as it was necessary to first obtain the class attribute and then process the string. Now 1. classList attributeFirst 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:
2. Practical Application In practice, column switching is a very common application. Continuing with the above example, when clicking a column, move <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 You may also be interested in:
|
<<: 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
The installation information on the Internet is u...
The default_server directive of nginx can define ...
Routing configuration commands under Linux 1. Add...
1. CSS file naming conventions Suggestion: Use le...
I have several tomcats here. If I use them at the...
I believe that many friends who are new to web pag...
The temperament of a web front-end website is a fe...
1. Two words at the beginning Hello everyone, my ...
Causes and consequences 1. When using the ansible...
So we introduce an embedding framework to solve th...
It has to be said that a web designer is a general...
Change the default style of select, usually throug...
The link-in style is to put all the styles in one...
1. Check the firewall status Check the firewall s...
MySQL UTF-8 encoding MySQL has supported UTF-8 si...