基于css实现
<!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>隔行换色之Css实现</title>
<style>
*{
margin: 0;
padding: 0;
}
ul,ol{
list-style: none;
}
.box{
width: 300px;
box-sizing: border-box;
margin: 20px auto;
}
.box li{
line-height: 50px;
border-bottom:1px dashed lightcoral;
}
.box li:nth-child(even){
background-color: lightpink;
}
.box li:hover{
background-color:lightskyblue;
}
</style>
</head>
<body>
<ul class="box" id="box">
<li>第一行</li>
<li>第二行</li>
<li>第三行</li>
<li>第四行</li>
<li>第五行</li>
</ul>
</body>
</html>
基于js实现

<!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>隔行换色之Css实现</title>
<style>
* {
margin: 0;
padding: 0;
}
ul,
ol {
list-style: none;
}
.box {
width: 300px;
box-sizing: border-box;
margin: 20px auto;
}
.box li {
line-height: 50px;
border-bottom: 1px dashed lightcoral;
}
</style>
</head>
<body>
<ul class="box" id="box">
<li>第一行</li>
<li>第二行</li>
<li>第三行</li>
<li>第四行</li>
<li>第五行</li>
</ul>
</body>
<script>
var box = document.getElementById('box');
var boxlist = box.getElementsByTagName('li')
for (var i = 0; i < boxlist.length; i++) {
boxlist[i].myindex = i;
boxlist[i].style.background = i % 2 === 0 ? "#FFF" : "#DDD";
boxlist[i].onmouseover = function () {
this.style.background = 'pink';
};
boxlist[i].onmouseout = function () {
var index = this.myindex;
boxlist[index].style.background = index % 2 === 0 ? "#FFF" : "#DDD";
};
}
</script>
</html>