1、html部分
<div id="box" class="box">
<div id="tags">
<div class="current">标签一</div>
<div>标签二</div>
<div>标签三</div>
<div>标签四</div>
</div>
<div id="content">
<div class="current">内容一</div>
<div>内容二</div>
<div>内容三</div>
<div>内容四</div>
</div>
</div>
2、css样式部分
<style type="text/css">
.box {
width: 1000px;
height: 500px;
margin: 100px auto;
}
.box #tags {
display: flex;
margin-top: 10px;
margin-left: 15px;
}
.box #tags div {
width: auto;
border-bottom: 3px solid red;
padding: 5px 10px;
cursor: pointer;
}
.box #tags .current {
background-color: skyblue;
border-left: 3px solid red;
border-top: 3px solid red;
border-right: 3px solid red;
border-bottom: none;
}
.box #content {
margin-left: 15px;
width: 800px;
height: 300px;
background-color: pink;
border-top: none;
border: 3px solid red;
margin-top: -3px;
}
.box #content div {
display: none;
font-size: 24px;
text-indent: 2em;
}
.box #content .current {
display: block;
}
</style>
3、js部分
<script type="text/javascript">
var box = document.getElementById('box');
var tags = box.children[0].children;
var content = box.children[1].children;
for (var i = 0; i < tags.length; i++) {
tags[i].index = i;
tags[i].onclick = function() {
for (var j = 0; j < tags.length; j++) {
tags[j].className = "";
content[j].className = "";
}
this.className = "current";
content[this.index].className = "current";
}
}
</script>