<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool教程(w3cschool.cn)</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;
}