js 隨機點名

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        body{
            background-color: aliceblue;
        }
        .box{
            width: 1000px;
            height: 240px;
            /*background-color: aqua;*/
            margin: 0 auto;
            margin-top: 100px;
            clear: both;
        }
        #btn{
            width: 100px;
            height: 30px;
            margin-left: 600px;
            margin-top: 50px;
        }
        .name{
            width: 100px;
            height: 30px;
            float: left;
            background-color: antiquewhite;
            margin-left: 10px;
            margin-top: 10px;
            text-align: center;
            line-height: 30px;
        }
        #span{
            float: right;
            position: relative;
            top: 55px;
            right: 185px;
        }
        h1{
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>隨機點名系統</h1>
    <span id="span"></span>
    <div class="box" id="box"></div>
    <input type="button" id="btn" value="點名"/>
    <span id="_span"></span>
    <script>
//        獲取id函數
        function my$(id){
            return document.getElementById(id)
        };
//        模擬後臺數據
        var arr = ["吉帆","靜慧","嘉歆","金枝","王大媽","老王","劉疙瘩","楊偉",
          "韓晁","靜涵","靜曼","晶晶","靚影","嘉寶","李靜","李志","李偉","家美"
          ,"金枝","靜恬","劉澤祥","靜和","晶靈","立超","嘉懿","王歡","王欣","郭琦"
          ,"嘉美","王琦","潔靜","小楊","佳思","吉玉","張煥","張賽","娟秀","菊月"
          ,"王晶","吉玟","王飛","皎潔","曾豔","王翠青"
      ];
//        動態建立層
        for(var i =0;i<arr.length;i++){
            var div = document.createElement("div");
            div.innerText=arr[i];
            div.className="name";
            my$("box").appendChild(div);
        };
        var _index;
//        點名
        my$("btn").onclick=function(){
            var peoples= arr.length;
//            監視按鈕的狀態
            if(this.value==="點名"){
//                定時針
                  timeId=setInterval(function () {
//                      清空全部層的顏色
                    for(var i =0;i<arr.length;i++){
                        my$("box").children[i].style.background=""
                    };
//                      留下當前層的顏色
                    var random = parseInt(Math.random()*peoples);
                    my$("box").children[random].style.background="red";

                    // _span
                    my$('_span').innerText = arr[random];
                    _index = random;
                },100);
                this.value="中止";
            }else{
                my$('_span').innerText = arr[_index];
//                清除計時器
                clearInterval(timeId);
                this.value="點名";
            };
        };

//        獲取時間的函數
        getTime();
        setInterval(getTime,1000)
        function getTime(){
            var day = new Date();
            var year = day.getFullYear();//
            var month = day.getMonth()+1;//
            var dat = day.getDate();//
            var hour = day.getHours();//小時
            var minitue = day.getMinutes();//分鐘
            var second = day.getSeconds();//
            month=month<10?"0"+month:month;
            dat=dat<10?"0"+dat:dat;
            hour=hour<10?"0"+hour:hour;
            minitue=minitue<10?"0"+minitue:minitue;
            second=second<10?"0"+second:second;
            my$("span").innerText=year+"-"+month+"-"+dat+" "+hour+":"+minitue+":"+second
        }


    </script>
</body>
</html>

總結:html

  主要是利用定時器,點擊開始IDE時候不斷的執行,並同時生成隨機數,利用數組的下標完成展現。數組

  主要用到的知識點:setInterval,Math.random()app

相關文章
相關標籤/搜索