One of the methods is to change the CSS class of page elements. In traditional JavaScript, we usually implement it by processing the classname feature of HTML Dom; jquery provides three methods to implement this function. Although they share the same idea with traditional methods, they save a lot of effort. Code . -"Jquery makes JavaScript code concise !"
1. addclass ()-add CSS class Copy codeThe Code is as follows: $ ("# 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 Copy codeThe Code is as follows: $ ("# 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.Copy codeThe Code is as follows: $ ("# 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 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 hasclass ("classname") method to determine whether an element has been assigned to a CSS class.