效果:
<body>
<input type="button" id="btn" value="点击变色"/>
<ul id="gh">
<li>红旗</li>
<li>五菱宏光</li>
<li>奔驰</li>
<li>哈佛</li>
</ul>
<script>
function my(id){
return document.getElementById(id);
}
my("btn").onclick=function(){
var list=my("gh").getElementsByTagName("li");
for (var i=0;i<list.length;i++) {
/*if (i%2==0) {
list[i].style.backgroundColor="red";
} else {
list[i].styl.backgroundColor="yellow";
}*/
list[i].style.backgroundColor=i%2 ?"red":"yellow";
}
}
</script>
</body>