JS一行代码,生成一个16进制随机颜色,简单粗暴。

   

   

var color = '#'+ Math.random().toString(16).substr(-6); 
document.body.style.backgroundColor = color;
这样 你每次刷新,就会呈现出不同的颜色。
  • Dont believe, just try.

   

Math.random().toString(16) 随机生成一个随机数,然后转为16进制字符串,截取后6位,now 就是随机颜色的后6位了。

 

   

   

原文地址:https://www.cnblogs.com/hill-foryou/p/8954452.html