<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0px;
margin: 0px;
list-style: none;
}
div {
width: 605px;
height: 338px;
margin: 120px auto;
position: relative;
}
ul {
overflow: hidden;
height: 40px;
width: 605px;
position: absolute;
top: 298px;
left: 0px;
}
li {
float: left;
width: 121px;
height: 40px;
background: #363636;
cursor: pointer;
color: white;
line-height: 40px;
text-align: center;
}
ul .current {
background: rgba(54, 54, 54, 0.6);
color: #ffda69;
}
img {
position: absolute;
top: 0px;
left: 0px;
display: none;
/* 隐藏标签 */
}
img.current {
display: inline-block;
/* 显示标签的时候,看标签的性质 */
}
</style>
</head>
<body>
<div>
<ul>
<li class="current">周年庆表现道具</li>
<li>全新游戏赛道</li>
<li>世冠小组赛</li>
<li>狄某有话说</li>
<li>西施这样玩</li>
</ul>
<img src="../images/pic01.png" alt="" class="current">
<img src="../images/pic02.png" alt="">
<img src="../images/pic03.png" alt="">
<img src="../images/pic04.png" alt="">
<img src="../images/pic05.png" alt="">
</div>
<script>
// 通过标签名查找HTML元素
var list = document.getElementsByTagName('li');
var images = document.getElementsByTagName('img');
for (var i = 0; i < list.length; i++) {
list[i].setAttribute('index', i);// 自定义标签属性
list[i].onclick = function () {//为所有li添加事件
for (var j = 0; j < list.length; j++) {
// 所有的标签变成正常
list[j].setAttribute('class', '');
images[j].setAttribute('class', '');
}
// 点击的时候,for循环已经执行完毕 i的值是list.length
// this在事件中,代表用户当前操作的标签
// list[i].setAttribute('class','');//书写方法错误
// images[i].setAttribute('class','');//书写方法错误
//修改选中的标签的属性
this.setAttribute('class', 'current');
var index = this.getAttribute('index');//获取自定义标签属性
images[index].setAttribute('class', 'current');
}
}
</script>
</body>
</html>