Latest web development tutorials

jQuery addClass () method

jQuery HTML / CSS Methods jQuery HTML / CSS Methods

Examples

The first <p> element to add a class name:

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

try it"

Definition and Usage

addClass () method to add one or more class names to the selected element.

This method does not remove the existing class property, just add one or more class names to the class attribute.

Tip: To add more than one class, use the class names separated by spaces.


grammar

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

参数 描述
classname 必需。规定一个或多个要添加的类名称。
function(index,currentclass) 可选。规定返回一个或多个待添加类名的函数。
  • index- 返回集合中元素的 index 位置。
  • currentclass- 返回被选元素的当前类名。


Examples

More examples

Add two classes to elements
How to add two elements to the specified class name.

Use the function add class
How to use the function to add the class to the selected elements.

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


jQuery HTML / CSS Methods jQuery HTML / CSS Methods