本文实例为大家分享了js实现简单抽奖功能的具体代码,供大家参考,具体内容如下
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" /> < title ></ title > < style > #box{ border: 1px solid aqua; height: 100px; width: 200px; text-align: center; line-height: 100px; margin: auto; font-size: 22px; } .active{ background: slateblue; } li{ text-align: center; list-style:none ; width: 40px; height: 20px; line-height: 20px; border: 1px solid blue; margin-left: 40px; float: left; } ul,li { list-style: none; } #shu { margin: auto; } </ style > </ head > < body > < button id = "stp" >开始</ button >< button id = "off" >停止</ button > < div id = "box" >恭喜< span >1</ span >号 </ div > < div id = "shu" >0</ div >< span >等奖</ span > < ul > < li class = "active" >1号</ li > < li >2号</ li > < li >3号</ li > < li >4号</ li > < li >5号</ li > < li >6号</ li > < li >7号</ li > </ ul > < script > var t=null; var ostp=document.getElementById("stp"); var ooff=document.getElementById("off"); var ospan=document.getElementsByTagName("div")[0].getElementsByTagName("span"); var oli=document.getElementsByTagName("ul")[0].getElementsByTagName("li"); var oshu=document.getElementsByClassName("shu"); function mytime(){ var n=Math.floor(Math.random()*7+1); ospan[0].innerText=n; ospan[1].innerText=oli[n-1].innerText; for(let i=0;i< oli.length ;i++) { oli[i] .className = "" ; } oli[n-1] .className = "active" ; } function mytime2() { var a = Math .floor(Math.random()*3+1); shu.innerText = a ; } ostp.onclick = function (){ clearInterval(t); t = setInterval (mytime,50); at = setInterval (mytime2,1000) } ooff.onclick = function (){ clearInterval(t); clearInterval(at); } </script> </ body > </ html > |
效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Alicesa/article/details/108436659