简单js实现选项卡

时间:2021-09-26 06:26:30
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>选项卡</title>
<style type="text/css">
*{margin: 0;padding: 0;}

#div1{
margin: 0 auto;
width: 800px;
height: 300px;
border: 1px solid black;
}
#div1 .div2{
width: 800px;
height: 70px;
display: block;

}
#div1 .div2 input{
margin: 0 1px;
border: none;
display: block;
width: 194px;
height: 70px;
float: left;
outline: none;

}
#div1 .div2 .active{
background-color: white;
}
#div1 div{
display: none;
}
</style>
<script type="text/javascript">
window.onload=function()
{
var oDiv=document.getElementById('div1')
var oBtn=document.getElementsByTagName('input');
var aDiv=oDiv.getElementsByTagName('div');

for(var i=0;i<oBtn.length;i++)
{
oBtn[i].index=i;
oBtn[i].onclick=function()
{
for(var i=0;i<oBtn.length;i++)
{
oBtn[i].className='';
aDiv[i+1].style.display='none';
}
this.className='active';
aDiv[this.index+1].style.display='block';
}
}
};
</script>
</head>
<body>
<div id="div1">
<div class="div2">
<input type="button" name="" id="" value="我的博客" class="active"/>
<input type="button" name="" id="" value="我的知识" />
<input type="button" name="" id="" value="我的动态" />
<input type="button" name="" id="" value="我的资料" />
</div>
<div style="display: block;">我的博客</div>
<div >我的知识</div>
<div >我的动态</div>
<div >我的资料</div>
</div>

</div>
</body>
</html>