等同于:hover 但是在一个盒子里代码量多的情况下 比较适用
<!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>
div {
height: 20px;
border: 1px solid pink;
}
.bg {
background-color: greenyellow;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
<script>
var box = document.querySelectorAll('div');
for (i = 0; i < box.length; i++) {
// 鼠标经过
box[i].onmouseover = function() {
this.className = 'bg'; // 新建的类名(鼠标经过后的效果
}
// 鼠标离开
box[i].onmouseout = function() {
this.className = ' '; //为空即可
}
}
</script>
</html>