zl程序教程

您现在的位置是:首页 >  前端

当前栏目

jquery学习之二属性(类)

2023-06-13 09:14:25 时间

addClass(class)

为每个匹配的元素添加指定的类名。
Addsthespecifiedclass(es)toeachofthesetofmatchedelements.

返回值

jQuery

参数

class(String):一个或多个要添加到元素中的CSS类名,请用空格分开

示例

为匹配的元素加上"selected"类

HTML代码:

<p>Hello</p>
jQuery代码:

$("p").addClass("selected");
结果:

[<pclass="selected">Hello</p>] 为匹配的元素加上selectedhighlight类

HTML代码:

<p>Hello</p> jQuery代码:

$("p").addClass("selectedhighlight"); 结果:

[<pclass="selectedhighlight">Hello</p>] ------------------------------------------------------------------------------------------------------------------------------

removeClass(class)

从所有匹配的元素中删除全部或者指定的类。 Removesallorthespecifiedclass(es)fromthesetofmatchedelements.

返回值

jQuery

参数

class(String):(可选)一个或多个要删除的CSS类名,请用空格分开

示例

从匹配的元素中删除"selected"类

HTML代码:

<pclass="selectedfirst">Hello</p> jQuery代码:

$("p").removeClass("selected"); 结果:

[<p>Hello</p>] 删除匹配元素的所有类

HTML代码:

<pclass="selectedfirst">Hello</p> jQuery代码:

$("p").removeClass(); 结果:

[<p>Hello</p>] ------------------------------------------------------------------------------------------------------------------------------

toggleClass(class)

如果存在(不存在)就删除(添加)一个类。 Addsthespecifiedclassifitisnotpresent,removesthespecifiedclassifitispresent.

返回值

jQuery

参数

class(String):CSS类名

示例

为匹配的元素切换"selected"类

HTML代码:

<p>Hello</p><pclass="selected">HelloAgain</p> jQuery代码:

$("p").toggleClass("selected"); 结果:

[<pclass="selected">Hello</p>,<p>HelloAgain</p>]