Div类,Javascript中的随机颜色生成器

时间:2016-08-10 13:11:52

标签: javascript html arrays

我不是Javascript专家,我需要一些帮助。 我在我的html中使用了9个div类,它显示为矩形框。

enter image description here

我想要这样的结果。当我刷新我的页面时,所有的颜色应该随机使用javascript更改,任何给我的好结果。 并给我代码。

1 个答案:

答案 0 :(得分:0)

它有点傻,希望你喜欢它,这就是你想要的。

<html>
    <head>
        <style>
            .divs{
                float:left; 
                width:30%; 
                height: 150px; 
                margin:5px;
            }
        </style>
    </head>
    <body onload="changeBackground();">
        <div>
            <div id="d1" class="divs"></div>
            <div id="d2" class="divs"></div>
            <div id="d3" class="divs"></div>
        </div>       
        <div>       
            <div id="d4" class="divs"></div>
            <div id="d5" class="divs"></div>
            <div id="d6" class="divs"></div>
        </div>       
        <div>        
            <div id="d7" class="divs"></div>
            <div id="d8" class="divs"></div>
            <div id="d9" class="divs"></div>
        </div>
        <script>
          function changeBackground() {
            var i=1;
            for(i=1;i<=9;i++){
            document.getElementById("d"+i).style.backgroundColor =
 '#'+Math.floor(Math.random()*16777215).toString(16); 
            }
        }
        </script>
    </body>
</html>`
相关问题