Source Code: (back to article)
<!DOCTYPE html>
<html>
<head>
<style>
.Class1 {
width: 150px;
height: 50px;
background-color: #1c87c9;
color: #e6ebef;
font-size: 25px;
padding: 5px;
}
.Class2 {
width: 300px;
height: 100px;
background-color: #8ebf42;
text-align: center;
font-size: 25px;
color: #eee;
margin-bottom: 10px;
}
</style>
</head>
<body>
<p>Klicken Sie auf den Button, um zwischen zwei Klassen hin-und herzuschalten.</p>
<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>
<div id="myDiv" class="Class1">
Ich bin ein Div Element.
</div>
<script>
function myFunction() {
document.getElementById("myDiv").classList.toggle("Class2");
}
</script>
</body>