[前端] html实现随机点名器的示例代码

2178 0
Honkers 2022-10-19 15:34:04 | 显示全部楼层 |阅读模式
此点名器开始点名后需点击停止按钮完成点名,因为是精简版没有考虑自动停止需求。姓名数据以字符串形式储存,适合小范围点名使用,有大量需求可自己适当改进。
  1. <head>
  2.     <meta charset="UTF-8">
  3.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4.     <title>随机点名生成</title>
  5.     <style>
  6.         /* 页面css样式 */
  7.         .wrapper {
  8.             width: 800px;
  9.             margin: 100px auto;
  10.             border: 1px solid #ddd;
  11.             text-align: center;
  12.         }
  13.         .box li {
  14.             vertical-align: top;
  15.             display: inline-block;
  16.             width: 100px;
  17.             height: 50px;
  18.             border: 2px solid #ddd;
  19.             border-radius: 15px;
  20.             text-align: center;
  21.             line-height: 50px;
  22.             margin: 5px;
  23.         }
  24.         .wrapper button {
  25.             border: none;
  26.             width: 100px;
  27.             height: 50px;
  28.             border-radius: 10px;
  29.             cursor: pointer;
  30.             outline: none;
  31.             margin-top: 20px;
  32.             font-weight: bolder;
  33.             color: #333;
  34.             background-color: rgb(14, 146, 43);
  35.         }
  36.         .wrapper button {
  37.             display: inline-block;
  38.         }
  39.         body {
  40.             background-color: #eee;
  41.         }
  42.     </style>
  43. </head>
  44. <body>
  45.     <div class="wrapper">
  46.      <h1 align="center">随机点名系统</h2>
  47.       //实时显示系统时间标签
  48.         <h6 id="data" align="right"></h6>
  49.         <ul class="box"></ul>
  50.         <button class="start">开始</button>
  51.         <button class="stop">停止</button>
  52.     </div>
  53. </body>
  54. <script>
  55.     //定义全局变量方便引用
  56.     var boxUl = document.getElementsByClassName('box')[0];
  57.     var start = document.getElementsByClassName('start')[0];
  58.     var stop = document.getElementsByClassName('stop')[0]
  59.     var oLi = document.getElementsByTagName('li');
  60.     //数据准备
  61.     var nameString = new String("张三,李四,王五,赵六,周七,田八,国九,归零,张3,李4,王5,赵6,周7,田8,国9,归0");
  62.     var nameArr = nameString.split(",");
  63.     //获取每个学生姓名添加到标签中,自动解析html标签
  64.     var str = "";
  65.     for (let i = 0; i < nameArr.length; i++) {
  66.         str += "<li >" + nameArr[i] + "</li>"
  67.     }
  68.     boxUl.innerHTML = str;
  69.     //添加开始按钮的点击事件
  70.     var timer = null;
  71.     start.onclick = function () {
  72.         // 设置定时器
  73.         timer = setInterval(function () {
  74.             // 根据数组长度范围生成随机数
  75.             var i = Math.floor(Math.random() * nameArr.length);
  76.             // 先通过for循环清空所有style属性
  77.             for (var j = 0; j < oLi.length; j++) {
  78.                 oLi[j].removeAttribute("style");
  79.             }
  80.             // 为随机选择的li颜色属性
  81.             oLi[i].style.background = "red";
  82.         }, 150);
  83.     };
  84.     // 点击停止
  85.     stop.onclick = function () {
  86.         // 清空定时器停止点名
  87.         clearInterval(timer);
  88.     }
  89.     //页面初始化时间设置
  90.     window.onload = function () {
  91.         datatime();
  92.     }
  93.     //页面时间动态刷新
  94.     setInterval(datatime, 1000);
  95.     function datatime() {
  96.         let data = new Date();
  97.         let dataString ="现在是北京时间:" + data.toLocaleString();
  98.         document.getElementById("data").innerHTML = dataString;
  99.     }
  100. </script>
复制代码
附一张效果图


到此这篇关于html实现随机点名器的示例代码的文章就介绍到这了,更多相关html随机点名器内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行