比如说我定义个变量A,这个A是随机生成的颜色代码,我想把这个A的值赋予一个DIV的背景颜色,这样我就可以每次刷新页面都得到不同颜色的DIV方块了。但是不知道应该怎么写。

解决方案 »

  1.   

    http://www.csrcode.cn/html/txdm/ljwb/3036.htm  看看这个
      

  2.   


    <div id="x" style="width:90px;height:90px">
    </div><script type="text/javascript">
    var d= "#"+Math.floor(Math.random()*Math.pow(2,24)).toString(16);
    document.getElementById("x").style.background=d;
    </script>
      

  3.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload = function() {
    var r = parseInt(Math.random().toString().substring(2)) % 256;
    var g = parseInt(Math.random().toString().substring(2)) % 256;
    var b = parseInt(Math.random().toString().substring(2)) % 256; //生成3个0-255的随机数
    document.getElementById('test').style.backgroundColor = 'rgb(' + r + ',' + g + ',' + b + ')';  //设置背景色
    }
    </script>
    </head><body>
    <div id="test" style="width:200px; height:200px;"></div>
    </body>
    </html>