JavaScript_9_练习:随机点名

效果图

代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>练习:随机点名</title>
  <style>
    div {
      margin: 50px auto;
      width: 500px;
      height: 300px;
      border: 3px solid #ccc;
      text-align: center;
    }

    div p {
      margin: 50px;
      font-size: 50px;
      color: red;
    }

    div button {
      margin: 20px;
      width: 90px;
      height: 40px;
    }
  </style>
</head>

<body>
  <div>
    <h2>随机点名</h2>
    <p>???</p>
    <button class="start">开始</button>
    <button class="end" disabled>结束</button>
  </div>
  <script>
    const arr = ['张三', '李四', '王五', '赵六', '冯七']
    const start = document.querySelector(".start")
    const end = document.querySelector(".end")
    const p = document.querySelector("p")
    let alter = 0
    let num = 0

    start.addEventListener("click", () => {
      alter = setInterval(() => {
        num = Math.floor(Math.random() * arr.length)
        p.innerText = arr[num]
      }, 25)
      start.disabled = true
      end.disabled = false
    })

    end.addEventListener("click", () => {
      clearInterval(alter)
      arr.splice(num, 1)
      if (arr.length === 1) {
        start.disabled = true
        end.disabled = true
      } else {
        end.disabled = true
        start.disabled = false
      }
    })
  </script>
</body>

</html>

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值