Home >Web Front-end >Front-end Q&A >How to change the class style of an element with css
CSS (Cascading Style Sheets) is an inevitable and important technology in Web development. It allows developers to separate the style of web pages from the page structure to achieve clearer and easier-to-maintain code. In CSS, class is an important selector that allows developers to assign specific attributes and values to elements to achieve customized style effects.
Sometimes, we need to dynamically modify the class of an element during JavaScript operations. For example, when the user clicks a button, the page needs to change the style based on the user's input or the status of the system. In CSS, changing the class of an element is very simple, just use the element.classList interface.
The element.classList interface is a new API in HTML5. It provides convenient methods to operate the class of elements, such as add(), remove() and toggle(), etc., so that developers can easily Add, delete, and modify styles to elements. Below I will introduce some commonly used classList interface methods and their usage.
The add() method can add a new class to the element's classList. If the class already exists in the element, then add() The method ignores this operation. The following is its syntax:
element.classList.add(class1 [, class2 [, ... [, classN]]])
Sample code:
const element = document.querySelector('#myDiv'); element.classList.add('myClass');
The remove() method can remove an existing class from an element Remove from the classList. If the class does not exist for the element, the remove() method will ignore the operation. The following is its syntax:
element.classList.remove(class1 [, class2 [, ... [, classN]]])
Sample code:
const element = document.querySelector('#myDiv'); element.classList.remove('myClass');
toggle() method can be added or moved in the element's classList Except a class, depends on whether the class already exists for the element. If the element does not have the class, the toggle() method adds it to the classList; if the element already has the class, the toggle() method removes it from the classList. The following is its syntax:
element.classList.toggle(class, true|false)
Among them, when the second parameter value is true, it means that the class is forcibly added; if the value is false, it means that the class is forcibly removed. Sample code:
const element = document.querySelector('#myDiv'); element.classList.toggle('myClass');
contains() method can check whether the classList of the element contains the specified class. If the class exists, it returns true ;If it does not exist, return false. The following is its syntax:
element.classList.contains(class)
Sample code:
const element = document.querySelector('#myDiv'); if (element.classList.contains('myClass')) { console.log('包含该class'); } else { console.log('不包含该class'); }
Summary:
Dynamicly changing the class of elements is a very important technology in the Web development process. It can make the interactive effect of web pages richer and more vivid. The element.classList interface provides convenient methods to operate the class of elements, allowing developers to easily add, delete, modify and check styles. This article introduces the four commonly used classList interface methods add(), remove(), toggle() and contains() and how to use them. I hope it will be helpful to everyone.
The above is the detailed content of How to change the class style of an element with css. For more information, please follow other related articles on the PHP Chinese website!