HTML DOM classList 属性
为 <div> 元素添加 class:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 300px;
height: 50px;
background-color: coral;
color: white;
font-size: 25px;
}
</style>
</head>
<body>
<p>点击按钮为 DIV 元素添加 "mystyle" 类。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<div id="myDIV">
我是一个 DIV 元素。
</div>
<script>
function myFunction() {
document.getElementById("myDIV").classList.add("mystyle");
}
</script>
</body>
</html>
定义和用法
classList 属性返回元素的类名,作为 DOMTokenList 对象。
该属性用于在元素中添加,移除及切换 CSS 类。
classList 属性是只读的,但你可以使用 add() 和 remove() 方法修改它。
浏览器支持
表格中的数字表示支持该属性的第一个浏览器的版本号。
| 属性 | |||||
|---|---|---|---|---|---|
| classList | 8.0 | 10.0 | 3.6 | 5.1 | 11.5 |
语法
element.classList
Properties
| 属性 | Description |
|---|---|
| length | 返回类列表中类的数量 该属性是只读的 |
方法
| 方法 | 描述 |
|---|---|
| add(class1, class2, ...) | 在元素中添加一个或多个类名。 如果指定的类名已存在,则不会添加 |
| contains(class) |
返回布尔值,判断指定的类名是否存在。
可能值:
|
| item(index) | 返回类名在元素中的索引值。索引值从 0 开始。 如果索引值在区间范围外则返回 null |
| remove(class1, class2, ...) | 移除元素中一个或多个类名。 注意: 移除不存在的类名,不会报错。 |
| toggle(class, true|false) | 在元素中切换类名。 第一个参数为要在元素中移除的类名,并返回 false。 如果该类名不存在则会在元素中添加类名,并返回 true。 第二个是可选参数,是个布尔值用于设置元素是否强制添加或移除类,不管该类名是否存在。例如: 移除一个 class: element.classList.toggle("classToRemove", false); 添加一个 class: element.classList.toggle("classToAdd", true); 注意: Internet Explorer 或 Opera 12 及其更早版本不支持第二个参数。 |
技术描述
| 返回值: | 一个 DOMTokenList, 包含元素的类名列表 |
|---|
更多实例
为 <div> 元素添加多个类:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 500px;
height: 50px;
padding: 15px;
border: 1px solid black;
}
.anotherClass {
background-color: coral;
color: white;
}
.thirdClass {
text-transform: uppercase;
text-align: center;
font-size: 25px;
}
</style>
</head>
<body>
<p>点击按钮为 DIV 元素添加多个类。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<div id="myDIV">
我是一个 DIV 元素。
</div>
<script>
function myFunction() {
document.getElementById("myDIV").classList.add("mystyle", "anotherClass", "thirdClass");
}
</script>
</body>
</html>
为 <div> 元素移除一个类:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 300px;
height: 50px;
background-color: coral;
color: white;
font-size: 25px;
}
</style>
</head>
<body>
<p>点击按钮移除 DIV 元素中的 "mystyle" 类.</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<div id="myDIV" class="mystyle">
我是一个 DIV 元素。
</div>
<script>
function myFunction() {
document.getElementById("myDIV").classList.remove("mystyle");
}
</script>
</body>
</html>
为 <div> 元素移除多个类:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 500px;
height: 50px;
padding: 15px;
border: 1px solid black;
}
.anotherClass {
background-color: coral;
color: white;
}
.thirdClass {
text-transform: uppercase;
text-align: center;
font-size: 25px;
}
</style>
</head>
<body>
<p>点击按钮移除多个 DIV 元素中的 类。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<div id="myDIV" class="mystyle anotherClass thirdClass">
我是一个 DIV 元素。
</div>
<script>
function myFunction() {
document.getElementById("myDIV").classList.remove("mystyle", "anotherClass", "thirdClass");
}
</script>
</body>
</html>
为 <div> 元素切换类:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 300px;
height: 50px;
background-color: coral;
color: white;
font-size: 25px;
}
.newClassName {
width: 400px;
height: 100px;
background-color: lightblue;
text-align: center;
font-size: 25px;
color: navy;
margin-bottom: 10px;
}
</style>
</head>
<body>
<p>点击按钮切换类名。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<div id="myDIV" class="mystyle">
我是一个 DIV 元素。
</div>
<script>
function myFunction() {
document.getElementById("myDIV").classList.toggle("newClassName");
}
</script>
</body>
</html>
获取 <div> 元素的类名:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 500px;
height: 50px;
}
.anotherClass {
background-color: lightblue;
}
.thirdClass {
text-align: center;
font-size: 25px;
color: black;
margin-bottom: 10px;
}
</style>
</head>
<body>
<p>点击按钮显示 div 元素的类名。</p>
<div id="myDIV" class="mystyle anotherClass thirdClass">
我是一个 DIV 元素,我使用了多个 类名。
</div>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myDIV").classList;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
查看 <div> 元素有多少个类名:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 500px;
height: 50px;
}
.anotherClass {
background-color: lightblue;
}
.thirdClass {
text-align: center;
font-size: 25px;
color: black;
margin-bottom: 10px;
}
</style>
</head>
<body>
<p>点击按钮显示 div 元素有多少个类名。</p>
<div id="myDIV" class="mystyle anotherClass thirdClass">
我是 DIV 元素,我有三个类
</div>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myDIV").classList.length;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
获取 <div> 元素的第一个类名(索引为0):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 500px;
height: 50px;
}
.anotherClass {
background-color: lightblue;
}
.thirdClass {
text-align: center;
font-size: 25px;
color: black;
margin-bottom: 10px;
}
</style>
</head>
<body>
<p>点击按钮显示div元素第一个类的类名 (索引为 0) 。</p>
<div id="myDIV" class="mystyle anotherClass thirdClass">
我是一个 DIV 元素,我有三个类
</div>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myDIV").classList.item(0);
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
查看元素是否存在 "mystyle" 类:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 500px;
height: 50px;
border: 1px solid black;
}
.anotherClass {
background-color: lightblue;
padding: 25px;
}
.thirdClass {
text-align: center;
font-size: 25px;
color: navy;
margin-bottom: 10px;
}
</style>
</head>
<body>
<p>点击按钮查看 DIV 元素是否有 "mystyle". 类。/p>
<div id="myDIV" class="mystyle anotherClass thirdClass">
我是一个 DIV 元
</div>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myDIV").classList.contains("mystyle");
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
查看元素是否存在 "mystyle" 类,如果存在则移除另外一个类名:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
.mystyle {
width: 500px;
height: 50px;
border: 1px solid black;
}
.anotherClass {
background-color: lightblue;
padding: 25px;
}
.thirdClass {
text-align: center;
font-size: 25px;
color: navy;
margin-bottom: 10px;
}
</style>
</head>
<body>
<p>点击按钮查看 DIV 元素是否有 "mystyle" 类,如果有移除 "anotherClass" 类。</p>
<div id="myDIV" class="mystyle anotherClass thirdClass">
我是一个DIV元
</div>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 9 及更早 IE 版本浏览器不支持 classList 属性。</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
if (x.classList.contains("mystyle")) {
x.classList.remove("anotherClass");
} else {
alert("不存在该类。");
}
}
</script>
</body>
</html>
相关文章
CSS 教程: CSS 选择器
CSS 参考手册: CSS .class 选择器
HTML DOM 参考手册: HTML DOM className 属性
HTML DOM 参考手册: HTML DOM getElementsByClassName() 方法
HTML DOM 参考手册: HTML DOM Style 对象
元素对象
元素对象