博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

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

Posted on 2019-09-25 14:02  PHP-张工  阅读(6504)  评论(0编辑  收藏  举报

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

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

使用这个方式可以生成随机颜色 #AF9838

'#' + Math.random().toString(16).substr(2, 6).toUpperCase();