代码-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>Document</title>
<!--编写CSS-->
<style>
*{
padding:0;/*重置所有内边距*/
margin:0;/*重置所有外边距*/
}
ul,li{
list-style: none;/*去掉ul,li自带小点*/
}
li{
display: inline-block;/*行内块*/
border:solid 1px pink;
background-color: pink;
width:80px;
height:26px;
line-height: 26px;/*行高*/
text-align: center;
cursor: pointer;/*鼠标手型*/
}
div{
width:300px;
height:150px;
background-color: skyblue;
border:solid 1px skyblue;
display: none; /*默认让所有的div都隐藏*/
}
div.content{
display: block;
}
li.table{
background-color: skyblue;
border: solid 1px skyblue;
}
</style>
</head>
<body>
<!--编写HTML-->
<ul>
<li class="table">犬夜叉</li>
<li>桔梗</li>
<li>奈落</li>
</ul>
<div class="content">犬夜叉模块</div>
<div>桔梗模块</div>
<div>奈落模块</div>
<!--编写JS-->
<script>
//找到li和div
var lis = document.getElementsByTagName('li');
var divs = document.getElementsByTagName('div');
//遍历数组
for(var i=0; i<lis.length; i++){
//给li对象添加属性并赋值
lis[i].index=i;
//绑定点击事件
lis[i].onclick=function(){
//遍历lis
for(var f=0; f<lis.length; f++){
//判断当前的下标
if(f == this.index){
//给当前下标设置className
lis[f].className='table';
divs[f].className='content';
}else{
//给当前下标清除className
lis[f].className='';
divs[f].className='';
}
}
}
}
</script>
</body>
</html>
Copyright [2018] by [羊驼可以吃吗] form [https://www.cnblogs.com/phpisfirst/]