jquery offers three ways to change the style of page elements, although they are interlinked with traditional methods, but save a lot of code
We often use JavaScript to change the style of page elements. One way is to change the CSS class (class) of the page element, which in traditional JavaScript is usually done by handling the classname feature of the HTML DOM, while jquery provides three ways to do this, Although they are interlinked with traditional methods, they save a lot of code. Or that sentence-"jquery makes JavaScript code simple!" ”
1. addclass ()-Add CSS Class
$ ("#target"). AddClass ("Newclass");
#target refers to the ID of the element for which you want to add a style
Newclass refers to the name of a CSS class
2. Removeclass ()-Removing CSS classes
$ ("#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 a CSS class
3. Toggleclass ()-Add or remove a CSS class: if the CSS class already exists, it will be removed and, conversely, if the CSS class does not exist, it will be added.
$ ("#target"). Toggleclass ("Newclass")
If the element with ID "target" already has a CSS style defined, it will be removed;
Conversely, the CSS class "Newclass" will be assigned to that ID.
In practice, we often define these CSS classes first, and then change the page element style by triggering JavaScript events, such as clicking a link. In addition, jquery provides a method Hasclass ("ClassName") to determine whether an element has been assigned to a CSS class.
The following is a complete example.
The code is as follows:
<! DOCTYPE html>