Home >Web Front-end >JS Tutorial >Example of usage of addClass() method in jQuery_jquery

Example of usage of addClass() method in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:22:101226browse

The example in this article describes the usage of the addClass() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This method adds one or more classes to the matching element.
This method has multiple grammatical forms.

Grammar structure 1:

Adds the specified class name to the matching element. If you want to add multiple class names at once, separate them with spaces.

Copy code The code is as follows:
$(selector).addClass(class)

Parameter list:

参数 描述
class 定义被添加类的名称

Example code:

Copy code The code is as follows:






addClass Function-Script Home





Script Home welcomes you




The above code can add two classes to the div, and can set the border and font style of the div.

Grammar structure 2:

Use the return value of the function as the name of the class to be added.

Copy code The code is as follows:
$(selector).addClass(function(index,oldclass))

Parameter list:

参数 描述
function(index,oldclass) 函数定义返回需要添加的一个或多个类名。
index - 可选,接受选择器的索引位置。
oldclass- 可选,接受选择器的当前的类名。

实例代码:

复制代码 代码如下:






addClass()函数-脚本之家





 
脚本之家欢迎您

 


上面代码和第一个实例的功能是一样的,只不过要添加的类是通过函数返回值得到的。

希望本文所述对大家的jQuery程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn