HTML随机点名程序

案例要求

1.点击点名按钮,名字界面随机显示,按钮文字由点名变为停止

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

案例源码

html 复制代码
<!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>

案例效果图

相关推荐
Setsuna_F_Seiei1 小时前
性能优化 - 列表分页查询的优化
前端·vue.js·性能优化
Setsuna_F_Seiei1 小时前
性能优化 - Vue.js 的懒加载的优化封装
前端·vue.js·性能优化
excel2 小时前
Prisma 查询不到 SQL 更新后的字段?先别急着重新打包
前端
狗头大军之江苏分军4 小时前
《潮水漫过十七岁》倒计时
前端·javascript·后端
小码哥0685 小时前
租房平台源码部署验收:房源样例、区域字典与看房预约逐项验证
前端·javascript·vue.js·租房小程序·租房系统·租房软件
htzyl2066 小时前
JS原型链与继承:class 底下到底是什么
前端·javascript·ecmascript 6
一条Go6 小时前
15 套品牌色 + 双模式:把颜色关进三层 token
前端·github
htzyl2066 小时前
JS-关于闭包捕获与释放
前端·javascript
狗头大军之江苏分军6 小时前
《潮水漫过十七岁》好久没写了
前端·javascript·后端
console.log('npc')6 小时前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习