分享

html选项卡

 学习的仓库 2016-01-05
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{
margin: 0;padding: 0;
}

.sty{
width: 200px;
height: 200px;
background-color: yellow;
font-size: 50px;
display:none;
}

button{
width:50px;
height:20px;
background-color: red;
}

</style>

<script type="text/javascript">
window.onload=function(){
var btns= document.getElementsByTagName("button");
var divs= document.getElementsByTagName("div");

for(var i=0;i<btns.length;i++){
//方法1:
// btns[i].index = i;
// btns[i].onclick=function(){
// for(var i=0;i<btns.length;i++){
// btns[i].style.backgroundColor="";
// divs[i].style.display="none";
// }
// btns[this.index].style.backgroundColor="yellow";
// divs[this.index].style.display="block";
// }

//方法2:
btns[i].onclick=function(){
for(var i=0;i<btns.length;i++){
if(this==btns[i]){
btns[i].style.backgroundColor="yellow";
divs[i].style.display="block";
}else{
btns[i].style.backgroundColor="";
divs[i].style.display="none";
}
}
}
}

}
</script>
</head>
<body>
<button >按钮1</button>
<button >按钮2</button>
<button >按钮3</button>
<button >按钮4</button>
<div class="sty" style="display:block;">1</div>
<div class="sty">2</div>
<div class="sty">3</div>
<div class="sty">4</div>
</body>
</html>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约