js雙色球,大樂透選獎
阿新 • • 發佈:2018-12-20
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,viewport-fit=cover"> <title>雙色球,大樂透選獎</title> <style> *{ margin:0; padding:0;} body{ background-color:#fffae7; } .shuang{ margin-top:100px; margin-left:20px; padding:10px; } .shuangbox{ color: #f00; } .lanqiu{ color: #00f; margin-left:30px; } p{ margin-top:20px; margin-left:20px; font-weight:bold;} </style></head><body><div> <button class="shuang" id="shuang">機選一注雙色球</button> <p> <span class="shuangbox" id="shuangbox"></span> <span class="lanqiu" id="lanqiu"></span> </p></div><div> <button class="shuang" id="daletou">機選一注大樂透</button> <p> <span class="shuangbox" id="daletoubox"></span> <span class="lanqiu" id="dale"></span> </p></div></body></html><script> window.onload=function(){ //未封裝版 信鬆哥 中大獎 $('shuang').onclick=shaungseqiu; shaungseqiu();//初始化 function shaungseqiu(){ $('shuangbox').innerHTML=arrChoose(33,6);//雙色球紅球 var lanqiu = Math.ceil(Math.random()*16); $('lanqiu').innerHTML=lanqiu;//雙色球藍球 } $('daletou').onclick=daletou; daletou();//初始化
function daletou(){ $('daletoubox').innerHTML=arrChoose(35,5);//大樂透紅球 $('dale').innerHTML=arrChoose(12,2);//大樂透藍球 } //maxlength 數字範圍最大值,long 選取的個數 function arrChoose(maxlength,long){ if(long>maxlength){return false} var arr=[]; for(var i=1;i<=maxlength;i++){ arr.push(i); } for(var i=0;i<3;i++){//打亂陣列三次 arr.sort(function(a,b){ return Math.random()>0.5?1:-1; }) } var newarr = arr.slice(0,long); newarr.sort(function(a,b){//對新陣列排序 return a-b; }) return newarr.join(' '); } function $(id){ return document.getElementById(id); } }</script>