需求:
1.点击界面中按钮,将当前被点击的按钮的背景颜色变为gold色,其他的按钮恢复默认。
2、当点击按钮的时候,需要将这个按钮对应位置的div显示出来
<style>
.tab_con {
width: 500px;
height: 350px;
margin: 50px auto 0;
}
.tab_btns {
height: 50px;
}
.tab_btns input {
width: 100px;
height: 50px;
background: #ddd;
border: 0px;
outline: none;
}
.tab_btns .active {
background: gold;
}
.tab_cons {
height: 300px;
background: gold;
}
.tab_cons div {
height: 300px;
line-height: 300px;
text-align: center;
display: none;
font-size: 30px;
}
.tab_cons .current {
display: block;
}
</style>
<body>
<div class="tab_con">
<div class="tab_btns">
<input type="button" value="按钮一" class="active">
<input type="button" value="按钮二">
<input type="button" value="按钮三">
</div>
<div class="tab_cons">
<div class="current">按钮一对应的内容</div>
<div>按钮二对应的内容</div>
<div>按钮三对应的内容</div>
</div>
</div>
</body>
<script>
// 1.获取元素
var inp = document.querySelectorAll('input');
var div = document.querySelectorAll('.tab_cons div');
// 2.遍历
for (var i = 0; i < inp.length; i++) {
// 给按钮inp 设置索引号
inp[i].setAttribute('index', i);
inp[i].addEventListener('click', function () {
for (var j = 0; j < inp.length; j++) {
inp[j].className = '';
div[j].className = '';
}
// 点击谁就得到相对应的类名
this.className = 'active';
// 下面和上面的对应
var index = this.getAttribute('index');
div[index].className = 'current';
})
}
</script>