(一)
生成从minNum到maxNum的随机数
1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title></title> 7 <!--1.引入jq--> 8 <script src="http://code.jquery.com/jquery-1.4.1.min.js"></script> 9 </head> 10 11 <body> 12 <!--2.html--> 13 <button>点我</button> 14 </body> 15 <script type="text/javascript"> 16 //3.js函数和事件 17 $("button").click(function(){ 18 var n = randomNum(1,5) 19 console.log(n) 20 }) 21 //生成从minNum到maxNum的随机数 22 function randomNum(minNum, maxNum) { 23 switch(arguments.length) { 24 case 1: 25 return parseInt(Math.random() * minNum + 1, 10); 26 break; 27 case 2: 28 return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10); 29 break; 30 default: 31 return 0; 32 break; 33 } 34 } 35 </script> 36 37 </html>
原文地址:https://www.cnblogs.com/wush-1215/p/8379178.html
时间: 2024-10-13 08:37:19