<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			
			.tab-head div{
				width: 80px;
				height: 40px;
				background-color: blue;
				margin-left: 30px;
				float: left;
			}
			/*.on代表选中的div*/
			.tab-head div.on{
				background-color: #E0FFFF;
			}
			.tab-body div{
				width: 200px;
				height: 100px;
				background-color:wheat;
				margin-left: 30px;
				display: none;
			}
			.tab-body div.on{
				/*使选中的div成块状显示出来*/
				display: block;
			}
		</style>
	</head>
	<body>
		<div class="tab-box">
		<div class="tab-head">
			<div id="t1" class="on" onclick="test(1)">1</div>
			<div id="t2" onclick="test(2)">2</div>
		</div>
		<div class="tab-body">
			<div id="k1" class="on">111</div>
			<div id="k2">222</div>
		</div>
		</div>
		<script type="text/javascript">
			//使用函数的方法进行取值
			function $(id){
				return document.getElementById(id);
			}
			function test(num){
				for(var i=1;i<=2;i++){
					//清除class值
					$("t"+i).className="";
					$("k"+i).className="";
				}
				//设置新的class值
				$("t"+num).className="on";
				$("k"+num).className="on";
			}
		</script>
	</body>
</html>
原文:https://www.cnblogs.com/weixin2623670713/p/12944228.html