function getRandomColor(){
      let colorStr = Math.ceil(0xffffff*Math.random()).toString(16);
      return '#' + colorStr.padStart(6,'0');
}

这个函数中值得注意的是:

1.js中0x开头表示16进制

2.toString(x)中x可以是任意除1以外正数,分别x进制格式输出

3.str.padStart(num,char)是es7的方法 

colorStr.padStart(6,'0')可以被替换成 '0'.repeat(6-colorStr.length)+colorStr

相关文章:

  • 2022-12-23
  • 2021-12-04
  • 2021-07-24
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-12-10
  • 2021-11-12
猜你喜欢
  • 2021-08-21
  • 2021-12-04
  • 2022-02-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案