zoukankan      html  css  js  c++  java
  • javascript小练习—点击将DIV变成红色(通过for循环遍历)

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title>点击将DIV变成红色</title>
    <style>
    body{font:12px/1.5 Tahoma;text-align:center;}
    code,input,button{font-family:inherit;}
    #div{width: 1000px;height: 240px;position: relative;}
    #div div{width: 200px;height: 200px;background-color: black;position: relative;float: left;margin: 10px;}
    button{cursor:pointer;}
    </style>
    <script>
    window.onload = function(){
    var oDiv = document.getElementById("div").getElementsByTagName("div");
    var oButton = document.getElementsByTagName("button")[0];
    oButton.onclick = function(){
    for(var i = 0;i<oDiv.length;i++){
    oDiv[i].style.backgroundColor = "red";
    }
    };
    };
    </script>
    </head>
    <body>
    <div id="div">
    <div></div>
    <div></div>
    <div></div>
    </div>
    <p><button>点击将DIV变成红色</button></p>
    </body>
    </html>
  • 相关阅读:
    协程
    多进程
    多线程
    模块进阶
    内建函数
    内建属性
    属性property
    私有化
    深拷贝、浅拷贝
    ==、is
  • 原文地址:https://www.cnblogs.com/zhaoyixin/p/4513636.html
Copyright © 2011-2022 走看看