首页 > Web开发 > 详细

JS隔行变色

时间:2015-12-09 11:24:31      阅读:225      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS隔行变色</title>
    <style>
    *{ padding: 0; margin: 0;}
    ul{ list-style: none;}
    body{ color: #333; font-size: 14px; font-family: "Microsoft Yahei"; background: #fff;}
    #box li{ padding: 10px; border-bottom: 1px solid #eee; -webkit-box-sizing: border-box; box-sizing: border-box;}
    #box li.hover{ background: #F6FBFF!important;}
    </style>
</head>
<body>
    <div id="box">
        <ul>
            <li>无序列表1</li>
            <li>无序列表2</li>
            <li>无序列表3</li>
            <li>无序列表4</li>
            <li>无序列表5</li>
            <li>无序列表6</li>
            <li>无序列表7</li>
            <li>无序列表8</li>
            <li>无序列表9</li>
            <li>无序列表10</li>
        </ul>
    </div>
    <script>
    window.onload = function(){
        
        var oDiv = document.getElementById(box);
        var aLi = document.getElementsByTagName(li);
        var that = null;

        for(var i = 0; i < aLi.length; i++){
            // 隔行变色(取模%)
            /*if(i%2 == 0){
                aLi[i].style.background = ‘#f9f9f9‘;
            }else{
                aLi[i].style.background = ‘‘;
            }*/

            // 或者用三目运算
            i%2 == 0 ? aLi[i].style.background = #f9f9f9 : aLi[i].style.background = ‘‘;

            // 方法一:鼠标经过,清空所有,当前高亮
            /*aLi[i].onmouseover = function(){
                for(var i = 0; i < aLi.length; i++){
                    aLi[i].className = ‘‘;
                }
                this.className = ‘hover‘;
            };*/

            // 方法二:利用that变量缓存当前的class
            aLi[i].onmouseover = function(){
                that = this.className;
                this.className = hover;
            };
            aLi[i].onmouseout = function(){
                this.className = that;
            }
        }
    };
    </script>
</body>
</html>

 

JS隔行变色

原文:http://www.cnblogs.com/bokebi520/p/5032107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!