<!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>
* {
margin: 0;
/* 外边距 */
padding: 0;
/* 内边距 */
}
#box {
text-align: center;
/* 元素文本的水平对齐方式 */
margin-top: 100px;
}
li {
display: inline-block;
width: 20px;
height: 20px;
padding: 5px;
border: 1px solid #000;
line-height: 20px;
text-align: center;
/* cursor: pointer; */
cursor: pointer;
/* 设置光标 corsshair 十字*/
}
li.active {
background-color: #000;
color: white;
}
</style>
</head>
<body>
<div id="box">
<ul>
<li onclick="clickHandler(0)" class="active">1</li>
<li onclick="clickHandler(1)">2</li>
<li onclick="clickHandler(2)">3</li>
<li onclick="clickHandler(3)">4</li>
<li onclick="clickHandler(4)">5</li>
</ul>
</div>
<script>
// 需求
// 1 每个li都可以点击
// 2 点击以后当期点击的li设置高亮
// 3 其他的li不会亮
var liS = document.querySelectorAll('li')
// console.log(liS, 'lis')
function clickHandler(index) {
// console.log(this)
// 被点击的li有active类名
for (var i = 0; i < liS.length; i++) {
liS[i].className = ''
}
liS[index].className = 'active'
}
</script>
</body>
</html>
08-17
360
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-10
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交