Js原生实现抽奖功能
<div>代码
按钮代码
JS原生代码
完整的代码:
<div style="width:365px;height:300px;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, 30) } </script>
<a href="javascript:oaction()" style="margin:5px;">开始抽奖</a>
<a href="javascript:clearInterval(sh)" style="margin:5px;">停止抽奖</a>