jQuery addClass()Method

jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Examples

Add a class name to the first <p> element:

$("button").click(function(){ $("p:first").addClass("intro"); });

Try it Yourself »

Definition and Usage

The addClass() method adds one or more class names to the selected elements.

This method does not remove existing class attributes, it simply adds one or more class names to the class attribute.

Tip:To add multiple classes, separate the class names with spaces.


Syntax

$(selector).addClass(classname,function(index,oldclass))

Parameter Description
classname Required. Specifies one or more class names to add.
function(index,currentclass) Optional. Specifies a function that returns one or more class names to add.
  • index- Returns the index position of the element in the collection.
  • currentclass- Returns the current class name of the selected element.


实例

More Examples

Add two classes to an element
How to add two class names to a specified element.

Add classes using a function
How to use a function to add classes to selected elements.

Change the class name of an element
How to use addClass() and removeClass() to remove a class name and add a new class name.


jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Other Extensions