css代码
<style>
*{
margin: 0;
padding: 0;
}
li{
list-style: none;
}
.tab{
display: flex;
width: 500px;
height: 30px;
align-items: center;
background-color: #eee;
border-bottom: 1px solid #f00;
}
.tab li{
flex: 1;
height: 30px;
line-height: 30px;
text-align: center;
cursor: pointer;
}
/* .tab li:nth-child(1){
background-color: #f00;
color: #fff;
} */
.tab li.active{
background-color: #f00;
color: #fff;
}
.showList{
display: flex;
width: 498px;
height: 200px;
border: 1px solid #000;
border-top: none;
}
.showList li{
width: 100%;
height: 100%;
display: none;
}
/* .showList li:nth-child(1){
display: block;
} */
.showList li.active{
display: block;
}
</style>
html代码
<div class="container">
<ui class="tab">
<li class="active">商品介绍</li>
<li>规格与包装</li>
<li>售后保障</li>
<li>商品评价</li>
<li>预售说明</li>
</ui>
<ui class="showList">
<li class="active">商品介绍模块</li>
<li>规格与包装模块</li>
<li>售后保障模块</li>
<li>商品评价模块</li>
<li>预售说明模块</li>
</ui>
</div>
script代码
<script>
// 获取元素
var tabLis = document.querySelectorAll('.tab li');
var showLis = document.querySelectorAll('.showList li');
for(var i = 0;i<tabLis.length;i++){
tabLis[i].setAttribute('index',i);
tabLis[i].onclick = function (){
for(var j = 0; j<tabLis.length;j++){
// tabLis[j].style.backgroundColor = 'transparent';
// tabLis[j].style.color = '#000';
// showLis[j].style.display = 'none';
tabLis[j].className = '';
showLis[j].className = '';
}
// this.style.backgroundColor = '#f00';
// this.style.color = '#fff';
this.className = 'active';
var showIndex = this.getAttribute('index');
// showLis[showIndex].style.display = 'block';
showLis[showIndex].className = 'active';
}
}
</script>