<div id="time" style="60px;height:60px;"></div> <script type="text/javascript"> // 思路:颜色由#、A-F、0-9组成 var str = "ABCDEF0123456789"; var color = "#"; for (var i = 0; i < 6; i++) { var index = Math.floor(Math.random() * str.length); color += str[index]; } var oDiv = document.querySelector("#time"); oDiv.style.backgroundColor = color; </script>