js 笔记 -- 随机生成颜色值

方法一:

 function  randowColor() {    
        var aColor = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f"];
        var len = aColor.length;
        var iColor = "#";
        var randowIndex = 0;
        while (iColor.length < 6) {
            randowIndex = Math.floor(Math.random() * len);
            iColor += aColor[randowIndex];
        }
        return iColor;
    }

方法二:

function randowColor() {
        var iColor = "#";
        while (iColor.length < 7) {
            var iRandow = Math.floor(Math.random() * 256);
            iRandow = iRandow.toString(16);
            // 5  8
            if (iRandow.length < 2) {
                iRandow = "0" + iRandow;
            }
            iColor += iRandow;
        }
        return iColor;
    }

posted @ 2013-08-29 09:43  hdchangchang  阅读(268)  评论(0编辑  收藏  举报