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>

案例效果图

相关推荐
小林学习编程5 分钟前
Springboot + vue + uni-app小程序web端全套家具商场
前端·vue.js·spring boot
柳鲲鹏6 分钟前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
weixin-a153003083161 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
ai小鬼头2 小时前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github
一只叫煤球的猫2 小时前
普通程序员,从开发到管理岗,为什么我越升职越痛苦?
前端·后端·全栈
vvilkim2 小时前
Electron 自动更新机制详解:实现无缝应用升级
前端·javascript·electron
vvilkim2 小时前
Electron 应用中的内容安全策略 (CSP) 全面指南
前端·javascript·electron
aha-凯心3 小时前
vben 之 axios 封装
前端·javascript·学习
遗憾随她而去.3 小时前
uniapp 中使用路由导航守卫,进行登录鉴权
前端·uni-app
xjt_09013 小时前
浅析Web存储系统
前端