js 随机颜色

  <div id="time" style="width: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>

 

 

posted @ 2020-11-09 21:47  云里知音  阅读(125)  评论(0编辑  收藏  举报