【js】随机色的2种实现方式

随机颜色的二种写法

写法一:
var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "A", "B","C","D"];
function Color(){
  var str = "#";
  for(var i=0;i<6;i++){
    str += arr[Math.floor(Math.random()*arr.length)]
  }
  return str;
}

写法二:
function Color() {
  var r = Math.floor(Math.random()*256),
  g = Math.floor(Math.random()*256),
  b = Math.floor(Math.random()*256);
  return "rgb("+r+","+g+","+b+")";
}

posted @ 2017-09-19 18:38  Mr_Welson  阅读(261)  评论(0)    收藏  举报