js入门基础7-2 (求模-隔行变色)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>隔行变色</title>
    <script>
        window.onload = function () {
            var aLi = document.getElementsByTagName('li');
            for (var i = 0; i < aLi.length; i++) {
                if (i % 2 == 0) {
                    aLi[i].style.background = '#333';
                }else{
                    aLi[i].style.background='red';
                }
            }
        }
    </script>
<body>
<ul>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>2</li>
    <li>1</li>
</ul>

</body>
</html>

 

posted @ 2015-11-19 16:15  webfby  阅读(176)  评论(0编辑  收藏  举报