javascript的选项卡

时间:2023-03-08 21:19:52

主要用的索引值

首先 写三个按钮

<input type="button" >
<input type="button" >
<input type="button" >

设置input按钮的样式

input{ border:; padding:3px 5px; background:blue; color:#fff; margin-left:5px;}

下面写切换的内容的html代码和css样式

<div id="div1">
<div style="display:block">第一个切换</div>
<div>第二个切换</div>
<div>第三个切换</div>
</div>
#div1 div{width:300px; height:200px; border:3px solid #000;display:none; padding:10px;}
.active{ background-color:red;}

开始写js代码

1  windows.onload = function(){
var oDiv = document.getElementById('div1')
var aInput = oDiv.getElementsByTagName('input');
var aDiv = oDiv.getElementsByTagName('div');
for(var i=0; i< aInput.length ; i++){
aInput[i].index = i;
aInput[i].oncilck = function(){
for( var i=0; i<aInput.length; i++){
this.className = '';
aDiv[this.index].style.display = 'none';
}
this.className = 'active';
aDiv[this.index].style.display = 'block';
}
}
}