search
HomeWeb Front-endHTML TutorialJavaScript 实现彩票中随机数组的获取_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>

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.