Instance
Add a class to the first P element:
$ ("button"). Click (function () { $ ("P:first"). AddClass ("Intro");});
Definition and usage
The AddClass () method adds one or more classes to the selected element.
The method does not remove the existing class attribute, adding only one or more class attributes.
Tip: If you want to add more than one class, use a space to separate the class name.
Grammar
$ (selector). addclass (class)
| Parameters |
Description |
| Class |
Necessary. Specify one or more class names. |
Using functions to add classes
Use a function to add a class to the selected element.
Grammar
$ (selector). addclass (function(index,oldclass))
$ (document). Ready (function () { $ ("button"). Click (function () { $ ("P:first"). AddClass ("intro note");}) ;
Removeclass () Method instance
Remove all <p> "Intro" classes:
$ ("button"). Click (function () { $ ("P:first"). Removeclass ("Intro");});
JQuery attribute Operations-addclass () and Removeclass () methods