Source Code: (back to article)
<!DOCTYPE html>
<html>
<head>
<style>
.Class1 {
width: 500px;
height: 50px;
border: 1px solid black;
}
.Class2 {
background-color: #eee;
padding: 25px;
}
.Class3 {
text-align: center;
font-size: 25px;
color: #1c87c9;
margin-bottom: 10px;
}
</style>
</head>
<body>
<p>Klicken Sie auf den Button, um herauszufinden, ob das Element Div eine Klasse von "Class1" hat.</p>
<div id="myDiv" class="Class1 Class2 Class3">
Ich bin ein Div-Element.
</div>
<button onclick="myFunction()">Klicken</button>
<p><strong>Note:</strong> Die Eigenschaft classList wird in Internet Explorer 9 und früheren Versionen nicht unterstützt.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myDiv").classList.contains("Class1");
document.getElementById("demo").innerHTML = x;
}
</script>
</body>