生成随机颜色
//随机颜色; function randColor() { return '#' + (function (color) { return (color += '0123456789abcdef'[Math.floor(Math.random() * 16)]) && (color.length == 6) ? color : arguments.callee(color); })('') } //console.log(randColor()); //$("#menu").attr("style", "background-color:" + randColor());
生成随机颜色数组
//获取随机颜色数组 function getColor(num) { var colorList = []; for (var i = 0; i < num; i++) { colorList.push(randColor()); } return colorList; } console.log(getColor(5));
<div id="menu"></div>
//获取随机颜色数组 function getColor(num) { for (var i = 0; i < num; i++) { var html = "<div style='height: 30px;line-height:30px;color:#000;text-align:center;background-color:" + randColor() + "'>" + randColor() + "</div>"; $("#menu").append(html); } } getColor(20);
Done!