js基础-点击切换div背景颜色

随机产生一个十六进制的颜色值,封装成一个函数,进行调用!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html, body {
            width: 100%;
            height: 100%;
        }

        div {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            min-height: 100%;
            height: auto;
            background: #fff;
        }
    </style>

</head>
<body>
<div onclick="chageColor(this)">
    点我试试
</div>
<script>
    function chageColor(obj) {
        function changeBgColor() {
            var str = "#";
            var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
            for (var i = 0; i < 6; i++) {
                var result = parseInt(Math.random() * 16);
                str += arr[result];
            }
            return str;
        }
    obj.style.background = changeBgColor();
    }
</script>
</body>
</html>

js基础-点击切换div背景颜色