使用HTML和JavaScript实现随机点名系统

下面是一个使用HTML和JavaScript实现的随机点名系统。这个系统可以从一个学生名单中随机选择一个名字,并在页面上显示出来。

1.功能说明:

这个应用程序使用纯HTML和JavaScript实现。

  • 包含一个输入框用于输入学生姓名,多个姓名之间用逗号分隔。
  • 提供"添加学生"按钮将输入的学生姓名添加到列表中。
  • 提供"随机点名"按钮从列表中随机选择一个学生姓名并显示在页面上。
  • 使用alert提示用户操作结果。

2.代码展示

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机点名系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f9;
            margin: 0;
        }
        .container {
            text-align: center;
            background-color: white;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            width: 400px;
        }
        h1 {
            margin-bottom: 30px;
            color: #333;
        }
        input[type="text"] {
            width: 80%;
            padding: 15px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 10px;
            font-size: 16px;
        }
        button {
            padding: 15px 30px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            font-size: 16px;
            margin: 0 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        #selected-name {
            font-size: 32px;
            margin-top: 30px;
            color: #333;
            font-weight: bold;
        }
        .message {
            margin-top: 20px;
            color: red;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>随机点名系统</h1>
        <input type="text" id="name-input" placeholder="请输入学生姓名,用逗号分隔">
        <br>
        <button onclick="addNames()">添加学生</button>
        <button onclick="pickRandomName()">随机点名</button>
        <div id="selected-name"></div>
        <div id="message" class="message"></div>
    </div>

    <script>
        let names = [];

        function addNames() {
            const input = document.getElementById('name-input').value;
            if (input.trim()) {
                const newNames = input.split(',').map(name => name.trim());
                names = [...names, ...newNames];
                document.getElementById('message').textContent = `已添加学生: ${newNames.join(', ')}`;
                setTimeout(() => {
                    document.getElementById('message').textContent = '';
                }, 3000);
                document.getElementById('name-input').value = '';
            } else {
                document.getElementById('message').textContent = '请输入有效的学生姓名';
                setTimeout(() => {
                    document.getElementById('message').textContent = '';
                }, 3000);
            }
        }

        function pickRandomName() {
            if (names.length === 0) {
                document.getElementById('message').textContent = '请先添加学生姓名';
                setTimeout(() => {
                    document.getElementById('message').textContent = '';
                }, 3000);
                return;
            }
            const randomIndex = Math.floor(Math.random() * names.length);
            const selectedName = names[randomIndex];
            document.getElementById('selected-name').textContent = `选中的学生是: ${selectedName}`;
        }
    </script>
</body>
</html>

3.效果展示

相关推荐
尘中客6 小时前
放弃 Echarts?前端直接渲染后端高精度 SVG 矢量图流的踩坑记录
前端·javascript·echarts·前端开发·svg矢量图·echarts避坑
2501_916007477 小时前
网站爬虫原理,基于浏览器点击行为还原可接口请求
前端·javascript·爬虫·ios·小程序·uni-app·iphone
Highcharts.js8 小时前
适合报表系统的可视化图表|Highcharts支持直接导出PNG和PDF
javascript·数据库·react.js·pdf
叫我一声阿雷吧8 小时前
JS 入门通关手册(35):执行上下文、调用栈与作用域链深度解析
javascript·作用域链·js进阶·执行上下文·调用栈·变量提升·闭包原理
奔跑的呱呱牛9 小时前
CSS Grid 布局参数详解(超细化版)+ 中文注释 Demo
前端·css·grid
Amumu1213810 小时前
Js:正则表达式(一)
开发语言·javascript·正则表达式
月光宝盒造梦师12 小时前
Ant Design Ellipsis 中的判断逻辑 isEleEllipsis 方法非常消耗性能
javascript·react·优化
gechunlian8813 小时前
SpringBoot3+Springdoc:v3api-docs可以访问,html无法访问的解决方法
前端·html
酉鬼女又兒13 小时前
零基础快速入门前端ES6 核心特性详解:Set 数据结构与对象增强写法(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·es6
我是伪码农14 小时前
HTML和CSS复习
前端·css·html