HTML:
  1. <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>
  2. <button type="button" onClick="cp_nums()">随机</button>
JS:


  1. function generate_randomx(count,nums_length) {
  2. //初始化数组
  3. var generated = new Array();
  4. //生成数组数
  5. var generatedCount = generated.length;
  6. //生成nums_length个随机数
  7. for(var i = 0 ; i < nums_length; i++){
  8. var candidate = Math.floor(Math.random() * count)+1;
  9. //如果生成一样的数字则重新生成
  10. for(var j = 0; j < generatedCount; j++) {
  11. if(candidate == generated[j]){
  12. candidate = Math.floor(Math.random() * count)+1;
  13. j= -1;
  14. }
  15. }
  16. generated[i] = candidate;
  17. generatedCount++;
  18. }
  19. return generated;
  20. }
  21. function sortNumber(a, b){
  22. return a - b
  23. }
  24. function creat_nums(caipiao_span,arr){
  25. var caipiao_span = document.getElementsByClassName(caipiao_span);
  26. for(var i = 0 ; i < caipiao_span.length; i++){
  27. var arr_nums = arr[i];
  28. if(arr_nums<10){
  29. arr_nums = '0'+String(arr[i]);
  30. }else{
  31. arr_nums = String(arr[i])
  32. }
  33. caipiao_span[i].innerText = arr_nums;
  34. }
  35. }
  36. function cp_nums(){
  37. var q = generate_randomx(35,5).sort(sortNumber);
  38. var h = generate_randomx(12,2).sort(sortNumber);
  39. creat_nums("caipiao35",q);
  40. creat_nums("caipiao12",h);
  41. }


DEMO