1. 程式人生 > >自動生成十六進位制顏色

自動生成十六進位制顏色

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        #box {
            width: 500px;
            height: 500px;
            margin: 
0 auto; background: red; } </style> <body> <div id="box"></div> <script> function getColor() { var str = "#"; //建立一個函式。因為裡面有number型別也有string型別的。所以統一用string型別 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 num = parseInt(Math.random() * 16); //建立一個從0-15的隨機數 str += arr[num]; } return str; } document.getElementById('box').style.backgroundColor = getColor(); </script> </body> </html>