js之选项卡2
2021-07-19 12:12
标签:har window ack button log 样式 .class meta win js之选项卡2 标签:har window ack button log 样式 .class meta win 原文地址:http://www.cnblogs.com/youbiao/p/7058678.htmlDOCTYPE HTML>
html>
head>
meta charset="utf-8">
title>无标题文档title>
style>
#div1 .active {background:yellow;}
#div1 div {width:200px; height:200px; background:#CCC; border:1px solid #999; display:none;}
style>
script>
window.onload=function ()
{
var oDiv=document.getElementById(‘div1‘); //获取id为div1的元素
var aBtn=oDiv.getElementsByTagName(‘input‘);
var aDiv=oDiv.getElementsByTagName(‘div‘);
for(var i=0;iaBtn.length;i++)
{
aBtn[i].index=i; //input元素的索引值设为i
aBtn[i].onclick=function ()
{
for(var i=0;iaBtn.length;i++) //将所有input的样式清空
{
aBtn[i].className=‘‘;
aDiv[i].style.display=‘none‘;
}
this.className=‘active‘; //将选择的元素添加class
//alert(this.index);
aDiv[this.index].style.display=‘block‘; //将选择项相应的div元素显示出来
};
}
};
script>
head>
body>
div id="div1">
input class="active" type="button" value="教育" />
input type="button" value="培训" />
input type="button" value="招生" />
input type="button" value="出国" />
div style="display:block;">1111div>
div>2222div>
div>333div>
div>4444div>
div>
body>
html>