1、效果图
2、代码
代码如下(js部分):
var sps = document.querySelectorAll('span')
var lis = document.querySelectorAll('li')
for (let i = 0; i < sps.length; i++) {
sps[i].onclick = function () {
sps.forEach(function (v) {
v.style.background = 'pink'
},
lis.forEach(function(v,k){
if(k==i){
v.style.display='block'
}else{
v.style.display="none"
}
})
)
sps[i].style.background = 'purple'
}
}
如下(html+css部分):
<div class="box" id="box">
<div class="hd">
<span class="current">体育</span>
<span>娱乐</span>
<span>新闻</span>
<span>综合</span>
</div>
<div class="bd">
<ul>
<li class="current">我是体育模块</li>
<li>我是娱乐模块</li>
<li>我是新闻模块</li>
<li>我是综合模块</li>
</ul>
</div>
</div>
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
}
.box {
width: 400px;
height: 300px;
border: 1px solid #ccc;
margin: 100px auto;
overflow: hidden;
}
.hd {
height: 45px;
}
.hd span {
display: inline-block;
width: 90px;
background-color: pink;
line-height: 45px;
text-align: center;
cursor: pointer;
}
.hd span.current {
background-color: purple;
}
.bd li {
height: 255px;
background-color: purple;
display: none;
}
.bd li.current {
display: block;
}
</style>