码迷,mamicode.com
首页 > Web开发 > 详细

几种实现隔行换色的写法(1中css其余js)

时间:2014-09-11 20:50:02      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   strong   for   

第一种: 

bubuko.com,布布扣
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>隔行变色</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div{
            margin:0 auto;
            width:80%;
            height:30px;
            border:1px solid #000;
            cursor: pointer;
        }
        .yellow{
            background-color: yellow;
        }
        .red{
            background-color: red;
        }
        .black{
            background-color: black;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <script>
        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            divs[i].index = i;
            if(i%2==0){
                divs[i].className = "red";
            }else{
                divs[i].className = "yellow";
            }
            divs[i].onmouseover = function(){
                this.className = "black";
            }
            divs[i].onmouseout = function(){
                if(this.index%2==0){
                    this.className = "red";
                }else{
                    this.className = "yellow";
                }
            }
        }
        
    </script>
</body>
</html>
隔行换色01

 

第二种:

bubuko.com,布布扣
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>隔行变色03</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div{
            margin:0 auto;
            width:80%;
            height:30px;
            border:1px solid #000;
            cursor: pointer;
        }
        .yellow{
            background-color: yellow;
        }
        .red{
            background-color: red;
        }
        .black{
            background-color: black;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <script>
        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            if(i%2==0){
                divs[i].className = "red";
            }else{
                divs[i].className = "yellow";
            }
            divs[i].onmouseover = function(){
                this.style.backgroundColor = "#000";
            }
            divs[i].onmouseout = function(){
                this.removeAttribute("style");                
            }
        }
    </script>
</body>
</html>
隔行换色02

第三种:

bubuko.com,布布扣
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>隔行变色04</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div{
            margin:0 auto;
            width:80%;
            height:30px;
            border:1px solid #000;
            cursor: pointer;
        }
        .yellow{
            background-color: yellow;
        }
        .red{
            background-color: red;
        }
        .black{
            background-color: black;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <script>
        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            divs[i].index = i;
            if(i%2==0){
                divs[i].className = "red";
            }else{
                divs[i].className = "yellow";
            }
            divs[i].addEventListener("mouseover",over,false); //这个特色就是事件监听机制
            divs[i].addEventListener("mouseout",out,false);
            function over(){
                this.className = "black";
            }
            function out(){
                if(this.index%2==0){
                    this.className = "red";
                }else{
                    this.className = "yellow";
                }
            }
        }
    </script>
</body>
</html>
隔行换色03

 

第四种: 

bubuko.com,布布扣
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>隔行变色05</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div{
            margin:0 auto;
            width:80%;
            height:30px;
            border:1px solid #000;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <script>
        var divs = document.getElementsByTagName("div");
        var oldColor;
        for (var i = 0; i < divs.length; i++) {
            if(i%2==0){
                divs[i].style.backgroundColor = "red";
            }else{
                divs[i].style.backgroundColor = "yellow";
            }
            divs[i].onmouseover = function(){
                oldColor = this.style.backgroundColor;  //这里的这个中间变量是这个方法的亮点
                this.style.backgroundColor = "#000";
            }
            divs[i].onmouseout = function(){
                this.style.backgroundColor = oldColor;                
            }
        }
    </script>
</body>
</html>
隔行换色04

 

第五种:

 

bubuko.com,布布扣
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>隔行变色02</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div{
            margin:0 auto;
            width:80%;
            height:30px;
            border:1px solid #000;
            cursor: pointer;
        }
        .yellow{
            background-color: yellow;
        }
        .red{
            background-color: red;
        }
        div:hover{
            background-color: black;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <script>
        //其实要是想用伪类的话,可以单纯用html和css就能做出隔行变色的效果
        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            if(i%2==0){
                divs[i].className = "red";
            }else{
                divs[i].className = "yellow";
            }
        }
        
    </script>
</body>
</html>
隔行换色05

 

 

 

 

  

几种实现隔行换色的写法(1中css其余js)

标签:style   blog   http   color   io   os   ar   strong   for   

原文地址:http://www.cnblogs.com/bjchenxn/p/3967139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!