jquery 学习之二 属性(类)

(编辑:jimmy 日期: 2025/10/30 浏览:2)

addClass(class)

为每个匹配的元素添加指定的类名。 Adds the specified class(es) to each of the set of matched elements.

返回值

jQuery

参数

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

示例

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

HTML 代码:

<p>Hello</p>

jQuery 代码:

$("p").addClass("selected");

结果:

[ <p class="selected">Hello</p> ]

为匹配的元素加上 selected highlight 类

HTML 代码:

<p>Hello</p>

jQuery 代码:

$("p").addClass("selected highlight");

结果:

[ <p class="selected highlight">Hello</p> ] ------------------------------------------------------------------------------------------------------------------------------

removeClass(class)

从所有匹配的元素中删除全部或者指定的类。 Removes all or the specified class(es) from the set of matched elements.

返回值

jQuery

参数

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

示例

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

HTML 代码:

<p class="selected first">Hello</p>

jQuery 代码:

$("p").removeClass("selected");

结果:

[ <p>Hello</p> ]

删除匹配元素的所有类

HTML 代码:

<p class="selected first">Hello</p>

jQuery 代码:

$("p").removeClass();

结果:

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

toggleClass(class)

如果存在(不存在)就删除(添加)一个类。 Adds the specified class if it is not present, removes the specified class if it is present.

返回值

jQuery

参数

class (String) :CSS类名

示例

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

HTML 代码:

<p>Hello</p><p class="selected">Hello Again</p>

jQuery 代码:

$("p").toggleClass("selected");

结果:

[ <p class="selected">Hello</p>, <p>Hello Again</p> ]  
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。