One way to do this is to change the CSS class (class) of page elements, which in traditional JavaScript is usually done by handling the classname features of the HTML DOM, and jquery provides three ways to do this. Although they are connected to traditional methods, they save a lot of code. Or the phrase-"jquery makes JavaScript code concise!" ”
1. AddClass ()-Add CSS class
Copy Code code as follows:
$ ("#target"). AddClass ("Newclass");
#target refers to the ID of the element that needs to add a style
Newclass refers to the name of the CSS class
2. Removeclass ()-Remove CSS class
Copy Code code as follows:
$ ("#target"). Removeclass ("Oldclass");
#target refers to the ID of the element that needs to be removed from the CSS class
Oldclass refers to the name of the CSS class
3. Toggleclass ()-Add or remove CSS classes: If the CSS class already exists, it will be removed, and conversely, if the CSS class does not exist, it will be added.
Copy Code code as follows:
$ ("#target"). Toggleclass ("Newclass")
If an element with the ID "target" already has a CSS style defined, it will be removed;
Conversely, the CSS class "Newclass" will be assigned to that ID.
4.hasClass ("ClassName")-determine if CSS already exists
In practice, we often define these CSS classes first, and then use JavaScript events to trigger (such as clicking on a link) to change the page element style. In addition, jquery provides a method Hasclass ("ClassName") to determine whether an element has been assigned to a CSS class.