封装一个函数 返回一个随机颜色 不传参数或者传true返回十六进制, 传false返回rgb模式
script>function Random(n, m) {if (n > m) {let temp = nn = mm = temp}return Math.floor(Math.random() * (m - n + 1)) + n}function getRandomColor(flag = true) {if (flag) {let str = '#'const arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']for (let i = 0; i < 6; i++) {str += arr[Random(0, arr.length-1)]}return str} else {let r = Random(0, 255)let g = Random(0, 255)let b = Random(0, 255)return `rgb(${r},${g},${b})`}}console.log(getRandomColor())body= document.querySelector('body')// console.log(getRandomColor())// console.log(getRandomColor(true))// console.log(getRandomColor(false))body.style.backgroundColor=getRandomColor()</script>