<!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>
js效果之table切换
最新推荐文章于 2023-08-10 18:55:59 发布