<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
.box {
width: 500px;
background-color: pink;
margin: 100px auto;
}
.box .head {
width: 100%;
height: 50px;
background-color: red;
}
.head ul li {
float: left;
width: 100px;
height: 50px;
background-color: aqua;
text-align: center;
line-height: 50px;
}
.bg {
background-color: blue !important;
}
.bd .item {
display: none;
}
</style>
</head>
<body>
<div class="box">
<div class="head">
<ul>
<li class="bg">商品介绍</li>
<li>规格包装</li>
<li>售后保障</li>
<li>商品评论</li>
<li>手机社区</li>
</ul>
</div>
<div class="bd">
<div class="item" style="display: block;">商品介绍模块</div>
<div class="item">规格包装模块</div>
<div class="item">售后保障模块</div>
<div class="item">商品评论模块</div>
<div class="item">手机社区模块</div>
</div>
</div>
<script>
var lis = document.querySelector('.head').querySelectorAll('li')
var items = document.querySelector('.bd').querySelectorAll('.item')
// 每个小li绑定点击事件
for(var i = 0; i<lis.length; i++) {
// 每一个li设置一个自定义属性
lis[i].setAttribute('index',i)
// 每个绑定点击事件
lis[i].onclick = function() {
for (var i = 0; i<lis.length; i++) {
lis[i].className = ''
}
this.className = 'bg'
// 点击拿到当前的自定义属性值
var index = this.getAttribute('index')
// 排他思想
for (var i = 0; i<items.length; i++) {
items[i].style.display = 'none'
}
// 显示下面的哪个模块
items[index].style.display = 'block'
}
}
</script>
</body>
</html>
03-22
7808
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)