物件導向的點名器

weixin_33866037發表於2017-05-24
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        #d1{
            width: 200px;
            height: 200px;
            border: 1px solid #ccc;
            margin: 100px auto;
        }
        #btn{
            width: 100px;
            height: 100px;
            margin: 0px 620px;
        }
    </style>
</head>
<body>
    <p>
        首先,先找變數,把變數給初始化,然後用原型定義方法,在這個方法裡寫要執行的內容,內容包括點選事件以及點選事件呼叫的函式。(記住要例項化和賦值),就好了!
    </p>
    <div id="dd">
        <div id="d1"></div>
        <button id="btn">點名</button>
    </div>
</body>
<script>
    window.onload=function(){
        var a1=['方丈','住持','劍俠客','小龍女','楊過','郭靖','郭鈺濤','流精欲','半藏','幼兒源','DVA','黑影','勇鍋','大姐','家林','假萌哼','航哥','劍閣'];
        var t1=new Tab('dd','d1','btn');
        t1.wc(a1);
    }
    var timer =null;
    function Tab(dd,d1,btn){
        this.oDiv=document.getElementById(dd);
        this.oDiv1=document.getElementById(d1);
        this.oBtn=document.getElementById(btn);
    }
    Tab.prototype.wc=function(arr){
        var that=this;
        this.oBtn.onclick=function(){
            if(that.oBtn.innerHTML=='點名'){
                timer = setInterval(run,50); 
                that.oBtn.innerHTML='暫停吧';
            }else{
                clearInterval(timer);
                that.oBtn.innerHTML='點名';
            }
        }
        function run(){
            var star=Math.round(Math.random()*18);
                that.oDiv1.innerHTML=arr[star];
        }
    }
</script>
</html>

相關文章