HTML+JavaScript随机名单抽取

全部代码,把数据源换成人名就行,写的比较乱,但能跑。

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <!-- <link rel="stylesheet" href="bootstrap.css"> -->
  <link rel="stylesheet" href="bootstrap.css">
  <style>
    *{margin: 0px;padding: 0px;}
    /* li{list-style: none;} */
    body{
      font-family: "楷体";
      user-select:none;
      background: url('2.jpg') no-repeat;
      background-size:100%;
      /*background-color: red;*/
    }
    .section{
      position: relative;
      width:1205px;
      height: 560px;
      background-color: rgba(0,0,0,.3);
      margin:165px auto 0;
      text-align: center;
    }
    .section h2{
      height: 90px;
      line-height: 90px;
      font-size: 40px;
      color:#fff;
    }
    .section .s-result{
      height: 400px;
      color: #fff;
    }
    .s-result .number{
      float: left;
      width: 895px;
      height: 300px;
      line-height: 300px;
      margin-left: 20px;
      font-size: 60px;
      font-weight: bold;
    }
    .btn p{
      line-height: 81px;
      font-size: 50px;
      color: #fff;
    }
    tr{
       font-size: 54px;
       height: 102px;
       line-height: 102px;
    }
  </style>
</head>
<body>
  <div class="section">
    <h2>看看谁最幸运!!!</h2>
    <div class="s-result">
      <table class="table table-bordered">
             <tr >
                  <td></td>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td></td>
            </tr>
            <tr>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
            </tr>
      </table>
      <audio  autoplay="autoplay">
        <source src="纯音乐-Victory (Film Music Prague现场).mp3" type="audio/ogg" />
      </audio>
      <div>
        <a class="btno btn  btn-primary">点 击 开 始</a>
        <a class=" btnu btn btn-primary">点 击 停 止</a>
      </div>
    </div>
    <div >
    </div>
  </div>
  <script>
    var res=false;
    var dis = 1;
    var oBtn = document.getElementsByClassName('btno')[0];
    var uBtn=document.getElementsByClassName('btnu')[0];
    var oResult = document.getElementsByClassName('s-result')[0];
    //数据源
    var arrName = ["1", "2", "3", "4", "5",
                   "6", "7","8", "9", "10", "11","12","13","14",
                   "15","16","17","18","19","20","21","22","23",
                   "24","25","26","27","28","29","30","31","32",
                   "33","34","35","36","37","38","39" ];     //抽签的内容
    var step = 1;
    var cnt = 1;
    var flag = true;
    //开始
    oBtn.onclick = function (){
      res=false;
      if(flag){
        step = 1;
        creatName();
        flag = false;
      }else{
        step = 1;
        creatName();
      }
    }
    //停止
    uBtn.onclick=function(){
        res=true;
    }
    //随机抽取一个人名
    function getName(){
      var num = Math.floor(Math.random()*(38));
      var n = arrName[num];
      return n;
    }
    function creatName(){
      if(step > cnt){
        return null;
      }
      step++;
       
      var oDiv = document.createElement('div');
      oDiv.className = 'number';
      oResult.appendChild(oDiv);

      var maxDis = 10;
      // alert(dis);
      var mySet = setInterval(function(){
        if(res){
          // oDiv.innerHTML = getName();
          if(dis){
          var td=document.getElementsByTagName('td');
          for(var i=0;i<td.length;i++)
          {
            var name=getName();
            var num=0;
            for(var j=0;j<td.length;j++)
            {
              if(td[j].innerText==name)
            {
              num++;
            }
            }
            if(num>0){

            }
            else
            {
              td[i].innerText=name;
            }
          }
          dis++;
          }
          else
          {
          var td=document.getElementsByTagName('td');
          for(var i=0;i<td.length;i++)
          {
            td[i].innerHTML = Birthday[i];
          }
          }
          clearInterval(mySet);
          creatName();
          return null;
        }else{
        var td=document.getElementsByTagName('td');
        for(var i=0;i<td.length;i++){
            td[i].innerHTML = getName();
            // console.log(dis);
          }
        }

      },50);
    }
  </script>
</body>
</html>

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学编程的大锟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值