<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
span {
display: inline-block;
width: 80px;
height: 30px;
background-color: #ccc;
line-height: 30px;
text-align: center;
cursor: pointer;
margin-bottom: 20px;
}
#box div {
display: none;
width: 300px;
height: 100px;
line-height: 100px;
text-align: center;
font-size: 30px;
border: 1px solid #ccc;
}
span.bg {
background-color: red;
color: white;
}
</style>
</head>
<body>
<div id="box">
<!-- /*初始化样式 bg*/ -->
<span class="bg">html</span>
<span>css</span>
<span>
JS
</span>
<div style="display: block;">HTML</div>
<div>CSS</div>
<div>JS</div>
</div>
<script>
var spans = document.querySelectorAll(‘span‘);
var divs = document.querySelectorAll(‘#box div‘);
for (var i = 0; i < spans.length; i++) {
spans[i].index = i;
//点击之后的排他思想
spans[i].onclick = function() {
for (var i = 0; i < spans.length; i++) {
spans[i].className = ‘‘;
divs[i].style.display = ‘none‘;
}
this.className = ‘bg‘;
divs[this.index].style.display = ‘block‘;
}
}
</script>
</body>
</html>