<style>
#box1,#box2{
width:300px;
height:400px;
}
h3{
height:40px;
line-height: 40px;
}
h3>a{
width:50px;
height:40px;
display: inline-block;
}
h3 a.active,h3 a:hover{
background: #eee;
color:#900;
}
#box1>.con,#box2>.con{
width:300px;
height:350px;
border:solid 1px #000;
position: relative;
}
#box1>.con>div,#box2>.con>div{
width:300px;
height:350px;
position: absolute;
top:0;
left:0;
display: none;
}
</style>
</head>
<body>
<div id="box1">
<h3 class="titles">
<a href="javascript:void(0)" class="active">标题1</a>
<a href="javascript:void(0)">标题2</a>
<a href="javascript:void(0)">标题3</a>
<a href="javascript:void(0)">标题4</a>
</h3>
<div class="con">
<div>内容1</div>
<div>内容2</div>
<div>内容3</div>
<div>内容4</div>
</div>
</div>
<p> </p>
<p> </p>
<p> </p>
<div id="box2">
<h3 class="titles">
<a href="javascript:void(0)" class="active">标题1</a>
<a href="javascript:void(0)">标题2</a>
<a href="javascript:void(0)">标题3</a>
<a href="javascript:void(0)">标题4</a>
</h3>
<div class="con">
<div>内容1</div>
<div>内容2</div>
<div>内容3</div>
<div>内容4</div>
</div>
</div>
- 显示第一个div;
- 鼠标悬浮在每一个a上时;/a[x].index=x;/a[x].οnmοuseοver=function
- 每个div隐藏起来;/显示当前div
<script>
window.onload=function(){
tabs("box2");
tabs("box1");
}
var as=document.getElementsByClassName("titles")[0].children;
var cons=document.getElementsByClassName("con")[0].children;
cons[0].style.display="block"; console.log("defult show cons[0]");
for(var x=0;x<as.length;x++){
as[x].index=x;
as[x].onmouseover=function(){
console.log(this.index);
for(var y=0;y<cons.length;y++){
cons[y].style.display="none";
} console.log(this.index);
cons[this.index].style.display="block";
}
}
</script>