js练习6(随机改变背景颜色)

<html>
    <head>
        <title>随机改变背景颜色(自动变化)</title>
        <style>
            #bg{
                width:200px;
                height:200px; 
            }
        </style>
    </head>
    <body>
        <div id="bg"></div>
        <script>
            function changeBg(){
                var bg=document.getElementById("bg");
                var rand=Math.round(Math.random()*1000000);
                bg.style.backgroundColor="#"+rand;
            }
            setInterval("changeBg();",1000);
        </script>
    </body>
</html>

 

你可能感兴趣的:(js)