Home >Web Front-end >JS Tutorial >Usage examples of toggleClass() method in jQuery_jquery

Usage examples of toggleClass() method in jQuery_jquery

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

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

This method toggles one or more classes that add or remove matching elements.
The toggleClass() method checks the specified class in the matching element. Adds the class if it does not exist, or removes it if it is set. This is called a toggle effect.

Grammar structure 1:

Copy code The code is as follows:
$(selector).toggleClass(class,switch)

Delete (add) a class if it exists (does not exist).

Parameter list:

参数 描述
class 规定添加或删除的类。
如果需要添加多个类,使用空格间隔。
switch 可选。布尔值,为true则加上对应的class,否则就删除。

实例代码:

复制代码 代码如下:






脚本之家









语法结构二:

复制代码 代码如下:
$(selector).toggleClass(function(index,class),switch)

Use the return value of the function as the switching class.

Parameter list:

参数 描述
function(index,class) 函数定义返回需要添加或删除的一个或多个类名。
index - 可选,接受选择器的索引位置。
class - 可选,接受选择器的当前的类。
switch 可选。布尔值,为true则加上对应的class,否则就删除。

实例代码:

复制代码 代码如下:






脚本之家









希望本文所述对大家的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