js生成随机颜色

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <style>
 8     #demo{
 9         width: 600px;
10         height: 600px;
11     }
12 </style>
13 <body>
14     <div id="demo"></div>
15 <script>
16     var div = document.getElementById("demo");
17     randomColor(div);
18     function randomColor(div) {
19         r = Math.ceil(Math.random()*255);
20         g = Math.ceil(Math.random()*255);
21         b = Math.ceil(Math.random()*255);
22         a = (Math.random()).toFixed(1);
23         div.style.background = "rgb(" + r + "," + g + "," + b + "," + a + ")";
24 }
25 </script>
26 </body>
27 </html>
posted @ 2020-01-06 11:05  xxThoedore  阅读(131)  评论(0编辑  收藏  举报