js选项卡

选项卡就是按钮与卡片绑定,给按钮添加事件,当点击这个按钮时,相应的卡片显示,其他的都不显示。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>选项卡</title>
<style>
#div1 .action{background:#FF0;}
#div1 div{200px; height:200px; background:#0F0; border:1px solid #000; display:none;}
</style>
<script>
window.onload=function (){
	var oDiv=document.getElementById('div1');
	var aBtn=oDiv.getElementsByTagName('input');//得到按钮的集合
	var aDiv=oDiv.getElementsByTagName('div');//得到div的集合
	for(var i=0;i<aBtn.length;i++){
		aBtn[i].index=i;//给按钮添加角标属性
		aBtn[i].onclick=function (){//用for循环给每个按钮添加事件
			 for(var i=0;i<aBtn.length;i++){//将所有按钮的class取消,全部不显示
				 aBtn[i].className='';
				 aDiv[i].style.display='none';
			 }
				 this.setAttribute('class','action');//按哪个按钮,就给哪个按钮添加class属性
				 aDiv[this.index].style.display='block';//相应的div框显示
		};
	}
}
</script>
</head>

<body>
<div id="div1" >
	<input class="action" type="button" value="教育"/>
	<input type="button" value="出国"/>
	<input type="button" value="培训"/>
	<input type="button" value="招生"/>
    <div style="display:block;">这是教育国选项卡</div><!--给第一个标签添加行间样式是为了让初始化状态中这个div显示,其他的都不显示-->
	<div>这是出国选项卡</div>
    <div>这是培训选项卡</div>
    <div>这是招生选项卡</div>
</div>
</body>
</html>
原文地址:https://www.cnblogs.com/lzzhuany/p/4560669.html