js效果之table切换

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>Document</title>
	<style type="text/css">
		*{
			padding: 0;
			margin:  0;
			list-style: none;
		}

		#box{
			width: 600px;
			height: 600px;
			border: 2px solid black;
			margin: 0px auto;
			margin-top: 50px;
		}
		li{
				width: 200px;
				height: 50px;
				float: left;
				font-size: 30px;
				line-height: 50px;
				text-align: center;
				
			}
			ul{
				overflow: hidden;
			}
			.con{
				width: 550px;
				height: 500px;
				margin: 0px auto;
				margin-top: 20px;
				display: none;
				position: relative;
			}
			.content{
				position: absolute;
				width: 550px;
				height: 500px;
				display: none;
			}

	</style>
	<script type="text/javascript">
		window.onload = function()
		{
			//抓所有的li
			var lis = document.getElementsByTagName('li');
			//抓3个内容
			var contents = document.getElementsByClassName('content');
			var con = document.getElementsByClassName('con')[0];
			//抓盒子
			var box = document.getElementById("box");
			
			//遍历li长度 加事件
			for (var i = 0; i < lis.length; i++) 
			{
				lis[i].index = i;  //给属性值保存起来
				lis[i].onmouseover = function()
				{
					var num = this.index;
					lis[num].style.color = 'black';  //经过那个让那个字体变黑色
					hidd(num);  //调用函数 让 所有内容隐藏,让 num的数字的 显示出来
				}
				lis[i].onmouseout = function()  //li的每个鼠标移除事件
				{
					var num = this.index;   //
					lis[num].style.color = '#fff'; //离开让字体变白色
				}
			}
			
			function hidd(num) //调用函数 让 所有内容隐藏,让 num的数字的 显示出来
			{
				for (var j = 0; j < contents.length; j++)  //遍历内容
				{
					contents[j].style.display = 'none';	//让所有内容隐藏
				}	
				
				contents[num].style.display = 'block'; // 让该显示的显示
			}
			
			box.onmousemove = function()
			{
				con.style.display = 'block';
			}
			
			box.onmouseout = function()
			{
				con.style.display = 'none';
			}
		}
	</script>
</head>
<body>
	<div id="box">
		<ul>
			<li style="background: red; color: white;">红色</li>
			<li style="background: yellow; color: white;">黄色</li>
			<li style="background: blue; color: white;">蓝色</li>
		</ul>
		
		<div class="con">
			<div class="content" style="background: red;">
				
			</div>
			<div class="content" style="background: yellow;">
				
			</div>
			<div class="content" style="background: blue;">
				
			</div>
		</div>
	</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值