jQuery属性操作之类样式操作
类样式的操作:指对DOM属性className进行添加、移除操作。比如addClass()、removeClass()、toggleClass()。
1. addClass()
1.1 概述
$(selector).addClass(className)
className
类型: String
为每个匹配元素所要增加的一个或多个样式名
返回值:jQuery
描述: 为每个匹配的元素添加指定的样式类名
1.2 为匹配到的元素添加指定类名
格式为: $(selector).addClass("className");
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>addClass Demo</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("div").addClass("divClass2");
})
</script>
</head>
<body>
<div class="divClass1"></div>
</body>
</html>
1.3 为匹配到的元素添加多个类名
格式为:
$(selector).addClass("className1 className2")
示例代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>addClass() Demo</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("div").addClass("divClass1 divClass2")
})
</script>
</head>
<body>
<div></div>
</body>
</html>
2. removeClass()
从所有匹配的元素中删除全部或者指定的类
2.1 移除指定的单个类
格式: $(selector).removeClass("className");
示例代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>removeClass Demo</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("div").removeClass("className1");
$("div").removeClass("className2");
})
</script>
</head>
<body>
<div class="className1 className2"></div>
</body>
</html>
2.2 移除指定的多个类
格式: $(selector).removeClass("className1 className2");
示例代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>removeClass() Demo</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("div").removeClass("className1 className2");
})
</script>
</head>
<body>
<div class="className1 className2"></div>
</body>
</html>
2.3 移除全部的类
格式:
$(selector).removeClass();
示例代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>removeClass() Demo</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("div").removeClass();
})
</script>
</head>
<body>
<div class="className1 className2"></div>
</body>
</html>
2.4 removeClass()案例, 解决“排他”问题
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>removeClass() Demo</title>
<style type="text/css">
.active{
color: red;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("ul li").click(function () {
$(this).addClass("active").siblings("li").removeClass("active");
})
})
</script>
</head>
<body>
<ul>
<li class="item">This is the first line.</li>
<li class="item">This is the second line.</li>
<li class="item">This is the third line.</li>
<li class="item">This is the fourth line.</li>
</ul>
</body>
</html>
3. toggleClass()
作用: 如果存在(不存在)就删除(添加)一个类
格式: $(selector).toggleClass("className");
示例代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>toggleClass() Demo</title>
<style type="text/css">
.active{
color: red;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function () {
$("p").click(function () {
$("p").toggleClass("active");
});
})
</script>
</head>
<body>
<p>This is the test.</p>
</body>
</html>
jQuery属性操作之类样式操作的更多相关文章
- JQuery DOM操作 、属性和CSS样式操作、其他函数
DOM操作 1.在div1内部最后追加一个节点 $("#div1").append("<img src='../01-HTML基本标签/img/Male.gif'/ ...
- jQuery学习之旅 Item3 属性操作与样式操作
本节将Dom元素的操作:属性操作.样式操作.设置和获取HTML,文本和值.Css-Dom操作. 1.属性操作 <input type="text" name="us ...
- jquery系列教程2-style样式操作全解
全栈工程师开发手册 (作者:栾鹏) 快捷链接: jquery系列教程1-选择器全解 jquery系列教程2-style样式操作全解 jquery系列教程3-DOM操作全解 jquery系列教程4-事件 ...
- jQuery 选择器 筛选器 样式操作 文本操作 属性操作 文档处理 事件 动画效果 插件 each、data、Ajax
jQuery jQuery介绍 1.jQuery是一个轻量级的.兼容多浏览器的JavaScript库. 2.jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方 ...
- jQuery 效果函数,jquery文档操作,jQuery属性操作方法,jQuerycss操作函数,jQuery参考手册-事件,jQuery选择器
jQuery 效果函数 方法 描述 animate() 对被选元素应用“自定义”的动画 clearQueue() 对被选元素移除所有排队的函数(仍未运行的) delay() 对被选元素的所有排队函数( ...
- jQuery属性遍历、HTML操作
jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery 遍历函数 jQuery 遍历函数包括了用于筛选.查找和串联元素的方法. .add() 将元素添加到匹配元素的集合中. . ...
- jQuery基础-选择器,样式操作
入口函数:ready() 当 DOM(文档对象模型) 已经加载,并且页面(包括图像)已经完全呈现时,会发生 ready 事件. 由于该事件在文档就绪后发生,因此把所有其他的 jQuery 事件和函数置 ...
- 6、前端--DOM操作(查找标签、节点操作、获取值操作、class操作、样式操作、绑定事件、内置参数this)
DOM操作之查找标签 前缀关键字>>>:document # 基本查找(核心) document.getElementById 根据ID获取一个标签 document.getElem ...
- jQuery入门教程-CSS样式操作大全
1.获取样式 2.设置样式 3.追加样式 4.移除样式 5.重复切换anotherClass样式 6.判断是否含有某项样式 7.设置 CSS 属性 参数 描述 name 必需.规定 CSS 属性的名称 ...
随机推荐
- git的常用指令(二) git add -A 、git add . 和 git add -u
git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文件内容修改(modified)以及新文件(new),但不包括被删除的文件. git add -u :他仅监控 ...
- springMVC 接受map参数的写法
<form > <input type="hidden" name="map['userKey']" value="11111&qu ...
- [转载]深入理解maven构建生命周期和各种plugin插件
我就不复制博主文章了,到原文地址看吧.写这个只是为了自己搜索起来方便些: https://blog.csdn.net/zhaojianting/article/details/80321488 htt ...
- Java虚拟机-------垃圾回收机机制
概述 jvm中的堆图 在了解 垃圾回收器 之前,首先得了解一下垃圾回收器的几个名词. 1. 吞吐量CPU 用于运行用户代码的时间与 CPU 总消耗时间的比值.比如说虚拟机总运行了 100 分钟,用户代 ...
- spring boot 使用RedisTemplate
1导入包 <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> ...
- Nginx访问限制配置
Nginx访问限制配置 nginx访问限制可以基于两个方面,一个是基于ip的访问控制,另一个是基于用户的信任登陆控制 下面我们将对这两种方法逐个介绍 基于IP的访问控制 介绍: 可以通过配置基于ip的 ...
- 第六篇.文件处理之python2和3字符编码的区别
目录 python2和3字符编码的区别 一.字符编码应用之python python2和3字符编码的区别 一.字符编码应用之python 1执行python的三个阶段 python test.py 执 ...
- python部署到服务器(2) 一一 nginx+uwsgi+Django
参考菜鸟教程,https://blog.csdn.net/qq_42314550/article/details/81805328, 和 https://www.cnblogs.com/chenice ...
- sourceforge.net
https://sourceforge.net/ SourceForge.net,又称SF.net,是开源软件开发者进行开发管理的集中式场所. SourceForge.net由VA Software提 ...
- docker使用上的错误
docker启动问题 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon ...