JS 随机颜色封装

<head>
    <meta charset="UTF-8">
    <title>color</title>
    <style>
        div{
            width: 200px;
            height: 100px;
            background-color: red;
        }

    </style>

    <script>
        function getHexColor() {
            var str = "#";
            var arr = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
            //产生随机数(0 - 15)没有16
            for (var i = 0;i < 6; i++){
                var num = [parseInt(Math.random()*16)]
                str+= arr[num];
            }
            return str;
        }
        window.onload=function () {
            document.getElementById("dv").style.backgroundColor = getHexColor();
        };
        console.log(getHexColor());

    </script>
</head>
<body>
    <div id="dv"></div>

</body>
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值