天天看點

js 生成随機顔色

function randomColor() {  
                var arrHex = ["0","1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d","e","f"],  
                    strHex = "#",  
                    index;  
                for(var i = 0; i < 6; i++) {  
                    index = Math.round(Math.random() * 15);  
                    strHex += arrHex[index];  
                }  
                return strHex;  
            }  
           

使用:将上面方法放到js當中,然後調用下面的方法,即可擷取随機生成的顔色

console.log(randomColor());
           

繼續閱讀