jQuery 属性操作 - toggleClass() 方法
实例
对设置和移除所有
元素的 "main" 类进行切换:
$("button").click(function(){
$("p").toggleClass("main");
});
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").toggleClass("main");
});
});
</script>
<style type="text/css">
.main
{
font-size:120%;
color:red;
}
</style>
</head>
<body>
<h1 id="h1">This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button class="btn1">切换段落的 "main" 类</button>
</body>
定义和方法
toggleClass() 对设置或移除被选元素的一个或多个类进行切换。
该方法检查每个元素中指定的类。如果不存在则添加类,如果已设置则删除之。这就是所谓的切换效果。
不过,通过使用 "switch" 参数,您能够规定只删除或只添加类。
语法
$(selector).toggleClass(class,switch)
参数
| 参数 | 描述 |
|---|---|
| class |
必需。规定添加或移除 class 的指定元素。 如需规定若干 class,请使用空格来分隔类名。 |
| switch | 可选。布尔值。规定是否添加或移除 class。 |
使用函数来切换类
$(selector).toggleClass(function(index,class),switch)
例子
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$('ul li').toggleClass(function(){
return 'listitem_' + $(this).index();
});
});
});
</script>
<style type="text/css">
.listitem_1, .listitem_3
{
color:red;
}
.listitem_0, .listitem_2
{
color:blue;
}
</style>
</head>
<body>
<h1 id="h1">This is a heading</h1>
<ul>
<li>Apple</li>
<li>IBM</li>
<li>Microsoft</li>
<li>Google</li>
</ul>
<button class="btn1">添加或移除列表项的类</button>
</body>
</html>
参数
| 参数 | 描述 |
|---|---|
| function(index,class) |
必需。规定返回需要添加或删除的一个或多个类名的函数。
|
| switch | 可选。布尔值。规定是否添加(true)或移除(false)类。 |
jQuery 属性操作 - toggleClass() 方法的更多相关文章
- jQuery 属性操作 - val() 方法
val() 方法返回或设置被选元素的值. 元素的值是通过 value 属性设置的.该方法大多用于 input 元素. 如果该方法未设置参数,则返回被选元素的当前值. <html> < ...
- jQuery 属性操作 - attr() 方法
定义和用法 attr() 方法设置或返回被选元素的属性值. 根据该方法不同的参数,其工作方式也有所差异. 实例1 设置被选元素的属性和值. <html><head><sc ...
- jQuery 属性操作 - addClass() 方法
使用 addClass() 和 removeClass() 来移除 class,并添加新的 class. <html> <head> <script type=" ...
- jquery——属性操作、特殊效果
1. attr().prop() 取出或者设置某个属性的值 <!DOCTYPE html> <html lang="en"> <head> &l ...
- jQuery 样式操作、文档操作、属性操作的方法总结
文档操作: addClass() 向匹配的元素添加指定的类名.after() 在匹配的元素之后插入内容.append() ...
- jQuery属性操作
jQuery 的属性操作的核心部分其实就是对底层 getAttribute().setAttributes()等方法的一系列兼容性处理 ...if ( notxml ) { name = name.t ...
- jQuery源代码学习之八——jQuery属性操作模块
一.jQuery属性模块整体介绍 jQuery的属性操作模块分四个部分:html属性操作,dom属性操作,类样式操作,和值操作. html属性操作(setAttribute/getAttribute) ...
- jQuery 属性操作和CSS 操作
如有在jQuery方法中涉及到函数,此函数必定会返回一个数值(函数由于运行次数不同触发一些不同效果) jQuery 属性操作方法(以下方法前些日子学习过,不再赘述) addClass() attr() ...
- JQuery --- 第二期 (jQuery属性操作)
个人学习笔记 1.JQuery的内容选择器 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
随机推荐
- Linux 系统 Composer 安装
Composer 是个包管理工具 在项目中使用它会很方便 本文中用 PHP 安装 1.下载安装 执行命令 curl -sS https://getcomposer.org/installer | ph ...
- git 删除错误commit
其实就是硬reset到之前对的commit记录然后强行再推送到远程库一下 具体操作: git reset --hard <commit_id> git push origin HEAD - ...
- Tensorflow细节-P196-输入数据处理框架
要点 1.filename_queue = tf.train.string_input_producer(files, shuffle=False)表示创建一个队列来维护列表 2.min_after_ ...
- YAML_07 有报错信息,告诉你错误忽略,继续执行下面的命令
ansible]# vim user5.yml --- - hosts: cache remote_user: root vars: user: bb tasks: - sh ...
- java文件夹上传下载组件
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- Matlab与数学建模
一.学习目标. (1)了解Matlab与数学建模竞赛的关系. (2)掌握Matlab数学建模的第一个小实例—评估股票价值与风险. (3)掌握Matlab数学建模的回归算法. 二.实例演练. 1.谈谈你 ...
- 【一起来烧脑】读懂Promise知识体系
知识体系 Promise基础语法,如何处理错误,简单介绍异步函数 内容 错误处理的两种方式: reject('错误信息').then(null, message => {}) throw new ...
- opendir,readdir,closedir
结构体dirent: struct dirent { ino_t d_ino; //inode number off_t d_off; //offset to the next diret unsi ...
- 线程池(3)-参数-实现ThreadFactory
1.介绍 ThreadFactory用来创建线程,需要实现newThread方法. 2.常用场景 线程重命名 设置守护进程 设置优先级 3.示例(线程重命名) public class ThreadF ...
- 4、vueJs基础知识04
简单的目录结构: |-index.html |-main.js 入口文件 |-App.vue vue文件(组件),官方推荐命名法(首字母大写) |-components 组件存放的文件夹 | ...