原生js选项卡

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>原生js选项卡</title>
<style>
*{
padding:0;margin:0;
}
.tab{
width:800px;
height:500px;
margin:64px auto;
background-color:rgba(122,133,122,0.4);
}
.tab_list ul{
width:800px;
height:60px;

}
.tab_list ul li{
width:200px;
height:60px;
background-color:rgba(122,133,122,0.5);
list-style:none;
float:left;
text-align:center;
line-height:60px;
font-size:20px;
color:white;
}
.tab_content{
width:800px;
height:440px;
position:relative;
}
.tab_content .item{
width:800px;
height:440px;
position:absolute;
display:none;
text-align:center;
line-height:440px;
color:#fff;
}
.tab_list .bgc{
background-color:red;
}
</style>
</head>
<body>
<div class="tab">
<div class="tab_list">
<ul>
<li class="bgc">aaaa</li>
<li class="">bbbb</li>
<li class="">cccc</li>
<li class="">dddd</li>
</ul>
</div>
<div class="tab_content">
<div class="item" style="display:block;">aaaa:this is a</div>
<div class="item">bbbb:this is b</div>
<div class="item">cccc:this is c</div>
<div class="item">dddd:this is d</div>
</div>
</div>
<script>
window.onload = function(){
//获取你要操作得元素
var lis = document.getElementsByTagName('li');
var items = document.getElementsByClassName('item');
console.log(lis);
console.log(items);
//遍历节点,给节点添加点击事件
for (var i=0;i<lis.length;i++){
//给下面content关联起来,需要自定义一个index属性
lis[i].index = i;
lis[i].onmousemove = function(){
//alert(i)
//再次for循环遍历,排空其他的 添加自己的展示类
for(var j=0;j < items.length; j++){
lis[j].className = "";
//同样的操作,给下面的内容添加展示先排空
items[j].style.display = 'none';
}
this.className = "bgc";
//添加相应的展示内容
items[this.index].style.display = 'block'
}
}
}
</script>
</body>
</html>
posted @ 2020-06-16 15:23  金牛座的女孩  阅读(11)  评论(0编辑  收藏  举报