JS 生成随机字符串 随机颜色

使用Math.random()生成随机数 0.7489584611780002
数字的.toString(n) 将数字转换为 n 进制的字符串 n取值范围(0~36)"0.vbpjw8lipf9"
使用 substr 截取去除前面的 0.
使用 toUpperCase() 转换为大写 L7NE21W7LMPdom

Math.random().toString(36).substr(2).toUpperCase();

使用这个方式能够生成随机颜色 #AF9838spa

'#' + Math.random().toString(16).substr(2, 6).toUpperCase();
相关文章
相关标签/搜索