JavaScript 实现彩票中随机数组的获取_html/css_WEB-ITnose
1.效果图:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Math.random方法彩票随机数的生成</title></head><body> <!-- 设置样式 --> <input type="text" id="text"> <button id="btnGo">开始</button> <button id="btnStop">获取随机数组</button> <script type="text/javascript"> //获取节点 var btnGo = document.getElementById("btnGo"); var btnStop = document.getElementById("btnStop"); var text = document.getElementById("text"); //定义生成最小到最大值的随机函数 function rand(min,max){ return parseInt(Math.random()*( max - min + 1) + min); } function start(min,max,length){ //定义空数组 var arr = []; while(arr.length<length){ //生成一个随机数prem var prem=rand(min,max); //判断生成的随机数prem是否在数组arr里,果然不在,就将这个随机数插入到数组里,如果在,执行下一次循环 if(arr.indexOf(prem) == -1){ arr.push(prem); } } //返回数组arr return arr; } var timer = 0; //单击开始按钮生成随机数组 btnGo.onclick =function(){ //清除 clearInterval(timer); timer = setInterval(function() { text.value = start(1,33,7); },50) } //单击停止按钮获取一组随机数 btnStop.onclick =function(){ clearInterval(timer); } </script></body></html>
2.复杂版
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Math.random方法彩票随机数的生成-升级版</title> <style type="text/css"> *{ margin: 0; padding: 0; } .wrap{ width: 600px; height: 300px; background-color: #f8e2e2; margin: 0 auto; } .list{ width: 440px; /*border: 1px solid red;*/ margin: 0px auto; } .list li{ list-style: none; width: 30px; height: 30px; display: inline-block; border: 1px solid #fff; border-radius: 30px; line-height: 30px; text-align: center; margin: 15px auto 15px; /*background-color: #f8f8f8;*/ /*background-color: rgba(255,255,255,1);*/ } .wrap p{ text-align: center; } .wrap p button{ text-align: center; width: 100px; } #setBtn{ background-color: red; color: #fff; border: none; } .active{ background-color: red; color: #fff; } </style></head><body> <div class="wrap" id="wrap"> <ul class="list"> <li>01</li> <li>02</li> <li>03</li> <li>04</li> <li>05</li> <li>06</li> <li>07</li> <li>08</li> <li>09</li> <li>10</li> <li>11</li> <li>12</li> <li>13</li> <li>14</li> <li>15</li> <li>16</li> <li>17</li> <li>18</li> <li>19</li> <li>20</li> <li>21</li> <li>22</li> <li>23</li> <li>24</li> <li>25</li> <li>26</li> <li>27</li> <li>28</li> <li>29</li> <li>30</li> <li>31</li> <li>32</li> <li>33</li> </ul> <p> <button id="setBtn">随机红球</button> <button id="clearBtn">清空</button> </p> </div> <script type="text/javascript"> var ballList = document.getElementById("wrap").getElementsByTagName("li"); var setBtn =document.getElementById("setBtn"); var clearBtn =document.getElementById("clearBtn"); //定义随机数组 function rnd(min, max) { return parseInt(Math.random()*(max - min + 1) + min); } function rndArray(min, max, length) { //先定义一个空数组 var arr = []; //生成一个长度为7的数组 while(arr.length < length) { //生成一个随机数 var rand = rnd(min, max); //判断生成的随机数rand是否在数组arr里,果然不在,就将这个随机数插入到数组里,如果在,执行下一次循环 if(arr.indexOf(rand) == -1) { arr.push(rand); } } arr.sort(function(a, b){return a - b;}) return arr; } function selectBall() { for(var j = 0; j < ballList.length; j++) { ballList[j].className = ""; } var arr = rndArray(1,33,7); // console.log(arr); for(var i = 0; i < arr.length; i++) { ballList[arr[i]-1].className = "active"; } } var timer = 0; setBtn.onclick = function() { clearTimeout(timer); timer = setInterval(selectBall,100); setTimeout(function() { clearTimeout(timer); },3000) // clearTimeout(timer); } clearBtn.onclick = function() { clearTimeout(timer); for(var j = 0; j < ballList.length; j++) { ballList[j].className = ""; } } </script></body></html>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...
