使用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.效果展示

相关推荐
qianmoQ21 分钟前
第五章:工程化实践 - 第三节 - Tailwind CSS 大型项目最佳实践
前端·css
C#Thread27 分钟前
C#上位机--流程控制(IF语句)
开发语言·javascript·ecmascript
椰果uu37 分钟前
前端八股万文总结——JS+ES6
前端·javascript·es6
~废弃回忆 �༄1 小时前
CSS中伪类选择器
前端·javascript·css·css中伪类选择器
IT、木易1 小时前
跟着AI学vue第五章
前端·javascript·vue.js
薛定谔的猫-菜鸟程序员1 小时前
Vue 2全屏滚动动画实战:结合fullpage-vue与animate.css打造炫酷H5页面
前端·css·vue.js
来一碗刘肉面2 小时前
TypeScript - 属性修饰符
前端·javascript·typescript
烂蜻蜓5 小时前
前端已死?什么是前端
开发语言·前端·javascript·vue.js·uni-app
Rowrey6 小时前
react+typescript,初始化与项目配置
javascript·react.js·typescript
祈澈菇凉11 小时前
Webpack的基本功能有哪些
前端·javascript·vue.js