<!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>
<link rel="stylesheet" href="css/wxhp.css">
</head>
<body>
<ul class="wxhp">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<span></span>
</ul>
<script>
// 获取所有li 得到一个数组
var wxhp = document.querySelectorAll(".wxhp li")
// 获取sqan标签
var span = document.querySelector(".wxhp span")
// 定义变量存储每一个对应的评论
var arr = ["差", "一般", "良好", "好", "优秀"]
// console.log(wxhp);
// 遍历wxhp
for (var i = 0; i < wxhp.length; i++) {
// 给每一个li添加一个自定义索引
wxhp[i].dataset.index = i
// console.log(wxhp[i]);
// 给每一个li添加鼠标划入事件
wxhp[i].onmouseover = function () {
将每一条评论添加到对应的后面
span.innerHTML= arr[this.dataset.index]
// console.log(arr[this.dataset.index]);
// console.log(1);
// 排他思想 将所有效果都去掉
for (var a = 0; a < wxhp.length; a++) {
wxhp[a].style.backgroundPositionY = "0"
}
//划过的时候前面的元素与自己都会执行 变成黄色五角星
for (var j = 0; j <= this.dataset.index; j++) {
wxhp[j].style.backgroundPositionY = "-29px"
}
}
}
</script>
</body>
</html>
素材