<style>div{width: 100px;height: 100px;border:1px solid red;}</style><div id="mydiv"></div><script>//返回一个随机数的颜色function getColor(){var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9","a", "b", "c", "d", "e","f"];var str = "#";for(var i=0;i<6;i++){//生成0~16的随机数var r = parseInt(Math.random()*16);str += arr[r];}return str;}//根据id找到对应的页面元素var div = document.getElementById("mydiv");//修改style里面backgroundColor/*<style>div{background-color : red;}</style>*/div.style.backgroundColor = getColor();</script>

