HTML随机点名程序

案例要求

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

 案例源码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Student Picker</title>
<style>
    body {
        font-family: Arial, sans-serif;
        background-color: #f4f4f4;
        margin: 0;
        padding: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
    }
    .container {
        text-align: center;
    }
    #nameDisplay {
        font-size: 24px;
        margin-bottom: 20px;
    }
    #toggleButton {
        background-color: #007bff;
        color: #fff;
        border: none;
        padding: 10px 20px;
        cursor: pointer;
    }
</style>
</head>
<body>

<div class="container">
    <div id="nameDisplay"></div>
    <button id="toggleButton">点名</button>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
    var students = ["小明", "小红", "小李", "小张", "小王", "小刚", "小花", "小美", "小华", "小军", "小强", "小丽", "小明", "小红", "小李", "小张", "小王", "小刚", "小花", "小美", "小华", "小军", "小强", "小丽"];

    var interval;
    var isPicking = false;

    $('#toggleButton').click(function() {
        if (isPicking) {
            clearInterval(interval);
            isPicking = false;
            $(this).text('点名');
        } else {
            isPicking = true;
            $(this).text('停止');
            interval = setInterval(function() {
                var randomIndex = Math.floor(Math.random() * students.length);
                $('#nameDisplay').text(students[randomIndex]);
            }, 100);
        }
    });
});
</script>
</body>
</html>

案例效果图

相关推荐

  1. Python随机点名

    2024-04-23 09:00:02       57 阅读
  2. 大学课堂点名程序

    2024-04-23 09:00:02       32 阅读
  3. C++如何获取随机点数

    2024-04-23 09:00:02       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 09:00:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 09:00:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 09:00:02       82 阅读
  4. Python语言-面向对象

    2024-04-23 09:00:02       91 阅读

热门阅读

  1. milvus 相似度检索的底层原理

    2024-04-23 09:00:02       34 阅读
  2. 【LeetCode热题100】【图论】腐烂的橘子

    2024-04-23 09:00:02       35 阅读
  3. SAM-Lighting 项目排坑

    2024-04-23 09:00:02       38 阅读
  4. 使用 Monaco Editor 开发 SQL 编辑器

    2024-04-23 09:00:02       32 阅读
  5. 什么是防火墙?

    2024-04-23 09:00:02       31 阅读
  6. asp.net get请求base64解密报错问题

    2024-04-23 09:00:02       32 阅读