【JavaScript】随机选号(大*透)
作者:广树 | 时间:2017-5-26 14:23:25 | 分类 : JavaScript/jQuery/Vue
HTML:
JS:
- <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>
- 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
赞一个0
发表评论: