ClassList:
1.ClassList属性有以下几种方法:
(1)通过ClassList可以获取元素的全部类名;
(2)通过ClassList.remove()可以删除元素的一个或多个类名;
(3)通过ClassList.add()可以向元素添加一个或多个类名;
(4)通过ClassList.lentgh可以获取到元素类名的数量;
(5)通过ClassList.item(x)可以获取到元素类名索引为x的类名;
(6)通过ClassList.toggle可以为元素切换类;
(7)通过ClassList.contains(x)查看元素是否存在类名为"x"的类;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.one {
width: 100px;
height: 100px;
background-color: red;
}
.bbc {
width: 100px;
height: 100px;
background-color: black;
}
</style>
</head>
<body>
<div class="one">1234556</div>
<script>
var div = document.querySelector(".one");
div.classList.remove("one");
div.classLIst.toggle(".bbc");
</script>
</body>
</html>
className 属性,设置或返回元素的 class 属性。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.name {
width: 100px;
height: 100px;
background-color: red;
}
</style>
<body>
<div></div>
<script>
var div = document.querySelector("div");
div.className = "name";
</script>
</body>
</html>
标签:color,ClassList,元素,100px,JS,ClassName,div,类名 From: https://www.cnblogs.com/ZhuAo/p/16857954.html