HTML5-Classlist样式操作
2024-09-18 23:01:23
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.red{
color:red
}
.green{
color: green;
}
.blue{
color: blue;
}
.underline{
text-decoration: underline;
}
</style>
</head>
<body>
<ul>
<li class="red">前端与移动开发</li>
<li class="blue">java</li>
<li>javascript</li>
<li class="red">c++</li>
</ul>
<input type="button" value="为第一个li元素添加样式" id="add">
<input type="button" value="为第二个li元素移除样式" id="remove">
<input type="button" value="为第三个li元素切换样式" id="toggle">
<input type="button" value="判断第四个li元素是否包含某个样式" id="contain">
<script>
window.onload=function(){
/*add:为元素添加指定名称的样式.一次只能添加一个样式*/
document.querySelector("#add").onclick=function(){
/*classList:当前元素的所有样式列表-数组*/
document.querySelector("li").classList.add("red");
document.querySelector("li").classList.add("underline");
//document.querySelector("li").className="red underline"
/*获取样式*/
var result=document.querySelector("li").classList.item(2);
console.log(result);
} /*remove:为元素移除指定名称的样式(不是移除class属性),一次也只能移除一个*/
document.querySelector("#remove").onclick=function(){
document.querySelector(".blue").classList.remove("blue");
} /*toggle:切换元素的样式:如果元素之前没有指定名称的样式则添加。如果有则移除*/
document.querySelector("#toggle").onclick=function(){
document.querySelectorAll("li")[2].classList.toggle("green");
} /*contains:判断元素是否包含指定名称的样式,返回true/false*/
document.querySelector("#contain").onclick=function(){
var isContain=document.querySelectorAll("li")[3].classList.contains("red");
console.log(isContain);
}
}
</script>
</body>
</html>
最新文章
- win10打开IL DASM步骤:
- Sql数据库查询当前环境有无死锁
- CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)
- 运用vertical:middle 使得元素垂直居中
- C语言每日一题之No.6
- android-exploitme(七):高级加密
- Spring 容器可以在自动装配相互协作的 bean 之间的关系,使用autowire属性定义指定自动装配模式。
- HTML学习笔记 CSS文本及字体及连接及列表(a标签使用及缩进) 案例 第七节 (原创)参考使用表
- python 迭代器,生成器
- luogu P5287 [HNOI2019]JOJO
- Vue学习之路---No.3(分享心得,欢迎批评指正)
- phpmyadmin详细的图文使用教程
- Py中查看数据类型【转载】
- <;spark入门>;<;Intellj环境配置>;<;scala>;rk入门>;<;Intellj环境配置>;<;scala>;
- sql,按照时间排序,取前N条
- HDFS 总结
- mysql 在linux下的完整安装过程
- Robot Framework分层、开发系统关键字
- Kibana TypeError : Object #<;GlobalState>; has no method &#39;setDefaults&#39;
- 《大规模web服务开发技术》笔记
热门文章
- java反射, 不看你可别后悔
- win10专业版Hyper-v下Docker挂载volume的方式使用Gitlab(汉化版)保存资料数据(使用外部redis)
- CSS高度坍塌原因及解决办法
- MySQL 保存镜像实战操作( 拷贝方法 )
- CCF CSP/CCSP报名费优惠的方法以及常见疑问
- Petrozavodsk Winter-2018. AtCoder Contest. Problem I. ADD, DIV, MAX 吉司机线段树
- [python 学习] lambda
- 学习python os commands socket模块
- Task8.循环和递归神经网络
- php str_ireplace()函数 语法