导航

萌即是正义!时不时分享一些ACG活动记录与有趣代码的小站!

侧边栏
最新评论
广树
2025-01-18 14:16
@Zrzzz:小小年纪就学会摔手柄了啊
Zrzzz
2025-01-18 12:44
@广树:玩splatoon的亲戚家小学生会摔我手柄 别问我怎么知道的😇
广树
2025-01-18 12:40
@老张博客:张总买?
广树
2025-01-18 12:40
@Zrzzz:是怕底部被磨损?鼠标天天磨好像也什么问题呀。
广树
2025-01-18 12:39
@石樱灯笼:😅再大就成显示器了
正在攻略

logo_kai.jpg


PSN奖杯卡

PSN奖杯卡

赞助商广告

【JavaScript】随机选号(大*透)

作者:广树时间:2017-05-26 14:23:25分类:JavaScript/jQuery/Vue

HTML:
<p class="caipiao_p"><span class="caipiao35">1</span><span class="caipiao35">2</span><span class="caipiao35">3</span><span class="caipiao35">4</span><span class="caipiao35">5</span><span>+</span><span class="caipiao12">1</span><span class="caipiao12">2</span></p>
<button type="button" onClick="cp_nums()">随机</button>
JS:


function generate_randomx(count,nums_length) {
  //初始化数组
  var generated = new Array();
  //生成数组数
  var generatedCount = generated.length;
  //生成nums_length个随机数
  for(var i = 0 ; i < nums_length; i++){
    var candidate = Math.floor(Math.random() * count)+1;
    //如果生成一样的数字则重新生成
    for(var j = 0; j < generatedCount; j++) {
      if(candidate == generated[j]){
        candidate = Math.floor(Math.random() * count)+1;
        j= -1;
      }
    }
    generated[i] = candidate;  
    generatedCount++;
  }
  return generated;  
}
function sortNumber(a, b){
	return a - b
}
function creat_nums(caipiao_span,arr){
	var caipiao_span = document.getElementsByClassName(caipiao_span);
	for(var i = 0 ; i < caipiao_span.length; i++){
		var arr_nums = arr[i];
		if(arr_nums<10){
			arr_nums = '0'+String(arr[i]);
		}else{
			arr_nums = String(arr[i])
		}
		caipiao_span[i].innerText = arr_nums;
	}
}
function cp_nums(){
	var q = generate_randomx(35,5).sort(sortNumber);
	var h = generate_randomx(12,2).sort(sortNumber);
	creat_nums("caipiao35",q);
	creat_nums("caipiao12",h);
	
}


DEMO

#JavaScript

donate.png

1210 x 50(蓝底).png

cloudcone