在css中,颜色可以使用 rgb(红色数值,绿色数值,蓝色数值) 表示
数值的范围是0-255,
通过Math生成随机数,产生不同的颜色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> div{ width: 300px; height: 400px; } </style> </head> <body> <div id="a1"></div> </body> <script> s=parseInt(Math.random()*256); b=parseInt(Math.random()*256); n=parseInt(Math.random()*256); color="rgb("+s+","+b+","+n+")" document.getElementById("a1").style.backgroundColor=color; </script> </html>