[转载]Javascript 随机抽奖 - SkySoot - 博客园

[转载]Javascript 随机抽奖 – SkySoot – 博客园.

<body>

    <div style="width:365px;height:180px;border:2px solid green" id="names">

    <script>

        for(var i=1;i<=40;i++){

            document.write('<div style="width:45px;height:35px;background:#ccc;float:left;border:2px solid white;text-align:center;padding-top:7px;">'+ i +'</div>')

        }

    </script>        

    </div>

    <a href="javascript:oaction()" style="margin:5px;" >开始抽奖</a>

    <a href="javascript:clearInterval(sh)" style="margin:5px;" >停止抽奖</a>

    <script>

        var names = document.getElementById("names").getElementsByTagName("div");

        var length = names.length;

        var oldNum = 0;

        var sh;

        function run(){

            var num = Math.floor((Math.random()*40));

            names[oldNum].style.background = "#ccc";

            oldNum = num;

            names[num].style.background="green";    

        }        

        

        function oaction(){

            sh=setInterval(run,10)

        }

    </script>

</body>
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏