手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >jquery 学习之二 属性(类)
jquery 学习之二 属性(类)
摘要:addClass(class)为每个匹配的元素添加指定的类名。Addsthespecifiedclass(es)toeachoftheset...

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>Hello</p> ]

为匹配的元素加上 selected highlight 类

HTML 代码:

<p>Hello</p>

jQuery 代码:

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

结果:

[ <p>Hello</p> ] ------------------------------------------------------------------------------------------------------------------------------ removeClass(class) 从所有匹配的元素中删除全部或者指定的类。 Removes all or the specified class(es) from the set of matched elements. 返回值

jQuery

参数

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

示例

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

HTML 代码:

<p>Hello</p>

jQuery 代码:

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

结果:

[ <p>Hello</p> ]

删除匹配元素的所有类

HTML 代码:

<p>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>Hello Again</p>

jQuery 代码:

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

结果:

[ <p>Hello</p>, <p>Hello Again</p> ]

【jquery 学习之二 属性(类)】相关文章:

AngularJS学习笔记之ng-options指令

jquery中添加属性和删除属性

jquery表单对象属性过滤选择器用法

JQuery中DOM事件绑定用法详解

Jquery注册事件实现方法

JQuery中DOM事件合成用法实例分析

jQuery实现强制cookie过期方法汇总

javascript实现table选中的行以指定颜色高亮显示

如何遍历对象的属性?

AngularJS学习笔记之基本指令

精品推荐
分类导航