首页 > 编程语言 > 详细

js中数组以及for循环的使用

时间:2015-07-23 00:28:08      阅读:243      评论:0      收藏:0      [点我收藏+]
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
div{
    width:200px;
    height:200px;
    float:left;
    border:1px solid #000;
    margin:20px;
    
    }
</style>
<script>
 window.onload=function()
    {
        var adiv=document.getElementsByTagName("div");
        /*alert (adiv.length);*/
        odiv[2].style.background="red";

    }

</script>
</head>

<body>
<div ></div>
<div ></div>
<div ></div>
<div ></div>
</body>
</html>

给4个div中的任何一个添加背景颜色

<script>
 window.onload=function()
    {
        var adiv=document.getElementsByTagName("div");
        /*alert (adiv.length);*/
        for(i=0;i<4;i++)
        {
            adiv[i].style.background="red" ;
            }

    }

</script>

利用for循环给4个div全部添加背景颜色。

js中数组以及for循环的使用

原文:http://www.cnblogs.com/kangshuai/p/4668963.html

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