Javascript 随机抽奖
开始抽奖 停止抽奖
Code:
<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>