table选项卡
需求:制作一个Table选项卡(不是CSS锚点的那种,用到了JavaScript),当鼠标滑过一个选项时显示对应部分的内容
步骤和代码如下:
CSS部分
<style>
body,ul {
padding: 0px;
margin: 0px;
}
.box {
height: 400px;
width: 500px;
margin: 100px auto;
border: 1px solid #ccc;
overflow: hidden;
}
ul {
width: 500px;
height: 40px;
list-style: none;
}
li {
float: left;
width: 100px;
height: 40px;
font: 500 15px/40px "microsoft yahei";
text-align: center;
background-color: #000;
color: #fff;
cursor: pointer;
}
span {
width: 400px;
height: 360px;
font: 700 150px/360px "simsun";
text-align: center;
display: none;
}
.show {
display: block;
}
.current {
background-color: #fff;
color: #000;
}
</style>
body部分
<div class="box">
<ul>
<li>幼儿园</li>
<li>小学</li>
<li>初中</li>
<li>高中</li>
<li>大学</li>
</ul>
<span>幼儿园</span>
<span>小学</span>
<span>初中</span>
<span>高中</span>
<span>大学</span>
</div>
<script>
//需求:鼠标放在li上面,li本身变色(添加类),对应的span也显示出来(添加类);
var liArr = document.getElementsByTagName("li");
var spanArr = document.getElementsByTagName("span");
for(var i=0; i<liArr.length; i++){
liArr[i].index = i; //利用索引值显示盒子
liArr[i].onmouseover = function(){
//排他思想
for(var j=0;j<liArr.length; j++){
liArr[j].className = "";
spanArr[j].className = "";
}
this.className = "current";
spanArr[this.index].className = "show";
}
}
</script>
效果如下图