// 传统写法 function randomColor1() { var r = Math.floor(Math.random() * 256), g = Math.floor(Math.random() * 256), b = Math.floor(Math.random() * 256); return `rgb(${r},${g},${b})`; } // 取巧 function randomColor2() { var color = '#' + Math.random().toString(16).substring(2,8); return color.length !== 7 ? randomColor2() : color; }
标签:floor,color,random,生成,randomColor2,随机,JS,256,Math From: https://www.cnblogs.com/wxxwjef/p/17296570.html