当前位置:   article > 正文

HTML随机点名程序

HTML随机点名程序

案例要求

1.点击点名按钮,名字界面随机显示,按钮文字由点名变为停止
2.再次点击点名按钮,显示当前被点名学生姓名,按钮文字由停止变为点名

 案例源码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Random Student Picker</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. background-color: #f4f4f4;
  11. margin: 0;
  12. padding: 0;
  13. display: flex;
  14. justify-content: center;
  15. align-items: center;
  16. height: 100vh;
  17. }
  18. .container {
  19. text-align: center;
  20. }
  21. #nameDisplay {
  22. font-size: 24px;
  23. margin-bottom: 20px;
  24. }
  25. #toggleButton {
  26. background-color: #007bff;
  27. color: #fff;
  28. border: none;
  29. padding: 10px 20px;
  30. cursor: pointer;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="container">
  36. <div id="nameDisplay"></div>
  37. <button id="toggleButton">点名</button>
  38. </div>
  39. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  40. <script>
  41. $(document).ready(function() {
  42. var students = ["小明", "小红", "小李", "小张", "小王", "小刚", "小花", "小美", "小华", "小军", "小强", "小丽", "小明", "小红", "小李", "小张", "小王", "小刚", "小花", "小美", "小华", "小军", "小强", "小丽"];
  43. var interval;
  44. var isPicking = false;
  45. $('#toggleButton').click(function() {
  46. if (isPicking) {
  47. clearInterval(interval);
  48. isPicking = false;
  49. $(this).text('点名');
  50. } else {
  51. isPicking = true;
  52. $(this).text('停止');
  53. interval = setInterval(function() {
  54. var randomIndex = Math.floor(Math.random() * students.length);
  55. $('#nameDisplay').text(students[randomIndex]);
  56. }, 100);
  57. }
  58. });
  59. });
  60. </script>
  61. </body>
  62. </html>

案例效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/502769
推荐阅读
相关标签
  

闽ICP备14008679号