* {
margin: 0;
padding: 0;
}
.box {
width: 400px;
height: 300px;
border: 1px solid #000;
margin: 100px auto;
}
.top {
list-style: none;
width: 100%;
height: 50px;
line-height: 50px;
text-align: center;
display: flex;
justify-content: space-between;
}
.top>li {
width: 80px;
height: 100%;
background: skyblue;
border-right: 1px solid #ccc;
}
.top>li:last-child {
border-right: none;
}
.bottom {
width: 100%;
height: 250px;
}
.bottom>.content {
width: 100%;
height: 100%;
display: none;
}
.bottom>.content:nth-child(1) {
background: red;
display: block;
}
.bottom>.content:nth-child(2) {
background: blue;
}
.bottom>.content:nth-child(3) {
background: green;
}
.bottom>.content:nth-child(4) {
background: yellow;
}
.bottom>.content:nth-child(5) {
background: purple;
}
.current {
background: red !important;
}
<div class="box">
<ul class="top">
<li class="current">新闻</li>
<li>视频</li>
<li>音乐</li>
<li>军事</li>
<li>财经</li>
</ul>
<div class="bottom">
<div class="content">新闻的内容</div>
<div class="content">视频的内容</div>
<div class="content">音乐的内容</div>
<div class="content">军事的内容</div>
<div class="content">财经的内容</div>
</div>
</div>
<script>
// 1.拿到需要操作的元素
let oItems = document.querySelectorAll("li");
let oDivs = document.querySelectorAll(".content");
// 定义变量保存上一次设置的索引
let previousIndex = 0;
// 2.给所有的选项卡添加点击事件
for (let i = 0; i < oItems.length; i++) {
let item = oItems[i];
// item.setAttribute("index", i);
item.onmousemove = function() {
// 清空上一次对应的元素对应的样式
let preItem = oItems[previousIndex];
preItem.className = "";
let preDiv = oDivs[previousIndex];
preDiv.style.display = "none";
// 设置当前这一次的元素对应的样式
// let index = this.getAttribute("index");
this.className = "current";
let oDiv = oDivs[i];
oDiv.style.display = "block";
// 保存当前这一次的索引
previousIndex = i;
};
}
</script>