<html> <head> <meta charset=utf-8> <head> <style> #dv{ width:300px; height:300px; background:pink } </style> </head> <body> <div id="dv"></div> <script>
function getColor(){ var str="#"; var arr=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","a","b","c","d","e","f"]; for(var i=0;i<6;i++){ var num=parseInt(Math.random()*22); str+=arr[num]; } return str; } console.log(getColor()); window.onload=function(){ document.getElementById("dv").style.backgroundColor=getColor(); }; </script> </body> </html>
|