js - 03课-01 隔行变色, 鼠标移入移出变色

1. 效果图

2. 源码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        li { height:24px; margin-bottom:3px; list-style:none; }
    </style>
</head>
<body>
<ul id="ul1">
    <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>

<script>
    window.onload = function(){
        var aLi = document.getElementsByTagName('li')
        var arrColor = ['red','yellow','blue']
        for(var i=0; i < aLi.length; i++){
            aLi[i].index = i;
            aLi[i].style.background = arrColor[i % arrColor.length]
            aLi[i].onmouseover = function(){
                this.style.background = 'gray';
            }
            aLi[i].onmouseout = function(){
                this.style.background = arrColor[this.index % arrColor.length]
            }
        }
    }
</script>
</body>
</html>

 

posted @ 2016-10-20 21:42  黑土白云  阅读(335)  评论(0编辑  收藏  举报