In webpage design, JavaScript is often used to change the style of page elements. One way is to change the CSS class of page elements. In traditional JavaScript, we usually process HTML domClassname
Jquery provides three methods to implement this function. Although they are similar to traditional methods, they save a lot of time.Code. -"Jquery makes JavaScript code concise !"
1. addclass ()-add CSS class
$ ("# Target"). Addclass ("Newclass");
// # Target indicates the ID of the element to add a style.
// Newclass refers to the CSS Class Name
2. removeclass ()-remove the CSS class
$ ("# Target"). Removeclass ("Oldclass");
// # Target indicates the ID of the element to be removed from the CSS class.
// Oldclass refers to the CSS Class Name
3. toggleclass ()-add or remove a CSS class: If the CSS class already exists, it will be removed. On the contrary, if the CSS class does not exist, it will be added.
$ ("# Target"). Toggleclass ("Newclass")
// If the element whose ID is "target" has defined the CSS style, it will be removed;
// Conversely, the CSS class "newclass" will be assigned to this ID.
4.Hasclass ("classname")-determine whether CSS already exists
In practical use, we often define these CSS classes first, and then use JavaScript event triggers (such as clicking a link) to change the page element style. In addition, jquery provides a methodHasclass ("classname ")
To determine whether an element has been assigned to a CSS class.