![效果图](https://img-blog.csdn.net/20170326233812309?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2RzMjA3MjA3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>隔行变色另一种方法</title>
<style>
li{
height: 24px;
margin-bottom: 3px;
list-style: none;
}
</style>
<script>
window.onload = function (){
var oUl = document.getElementById('list');
var aLi = oUl.getElementsByTagName('li');
var aColor = ['#f00', '#0f0', '#00f'];
var oldColor = '';
for(var i=0; i<aLi.length; i++){
aLi[i].style.background = aColor[i%aColor.length];
aLi[i].onmouseover = function (){
oldColor = this.style.background;
this.style.background = '#000';
};
aLi[i].onmouseout = function (){
this.style.background = oldColor;
};
}
};
</script>
</head>
<body>
<ul id="list">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>隔行变色</title>
<style>
li{
height: 24px;
margin-bottom: 3px;
list-style: none;
background-color:
}
</style>
<script>
window.onload = function (){
var oUl = document.getElementById('list');
var aLi = oUl.getElementsByTagName('li');
var aColor = ['#f00', '#0f0', '#00f'];
for(var i=0; i<aLi.length; i++){
aLi[i].index = i;
aLi[i].style.backgroundColor = aColor[i%aColor.length];
aLi[i].onmouseover = function(){
this.style.backgroundColor = '#000';
};
aLi[i].onmouseout = function(){
this.style.backgroundColor = aColor[this.index%aColor.length];
};
}
};
</script>
</head>
<body>
<ul id="list">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>