当前位置: 首页 > news >正文

js考核第五题

题五:动态表格

要求:

1.表格由专业班级学号1-10号同学的信息组成,包括:学号、姓 名、性别、二级学院、班级、专业、辅导员;

2.表格的奇数行字体为黑色,底色为白色;偶数行字体为白色,底 色为黑色;

3.表格的每一行后有一个删除按钮,点击后会跳出提示弹窗,确认后删除该行的内容,并且删除后上述的颜色规律保持不变:

4.表格的右上方有一个添加按钮,点击后跳出一个表单弹窗,可以填加新的学生的信息。

html

<body>
    <div class="table-container">
        <button id="add-btn" class="add-btn">添加学生</button>

        <table class="student-table">
            <thead>
                <tr>
                    <th>学号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>二级学院</th>
                    <th>班级</th>
                    <th>专业</th>
                    <th>辅导员</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>张三</td>
                    <td>男</td>
                    <td>计算机学院</td>
                    <td>1班</td>
                    <td>软件工程</td>
                    <td>赵老师</td>
                    <td><button class="delete-btn">删除</button></td>
                </tr>
                <tr>
                    <td>2</td>
                    <td>李四</td>
                    <td>女</td>
                    <td>信息学院</td>
                    <td>2班</td>
                    <td>网络工程</td>
                    <td>王老师</td>
                    <td><button class="delete-btn">删除</button></td>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="form-popup" id="add-form">
        <h3>添加学生信息</h3>
        <input type="text" id="student-id" placeholder="学号">
        <input type="text" id="student-name" placeholder="姓名">
        <input type="text" id="student-gender" placeholder="性别">
        <input type="text" id="student-college" placeholder="二级学院">
        <input type="text" id="student-class" placeholder="班级">
        <input type="text" id="student-major" placeholder="专业">
        <input type="text" id="student-counselor" placeholder="辅导员">
        <button id="submit-add">提交</button>
        <button id="cancel-add">取消</button>
    </div>

css

 body {
            margin: 0;
            padding: 0;
            background-color: #fff;
        }

       .table-container {
            max-width: 1200px;
            margin: 50px auto;
            padding: 20px;
            background-color: #fff;
            position: relative;
        }

       .add-btn {
            position: absolute;
            top: 0px;
            right: 0px;
            padding: 10px 20px;
            background-color: #28a745;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 4px;
        }

       .add-btn:hover {
            background-color: #218838;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th,
        td {
            padding: 10px;
            text-align: left;
            border: 1px solid #ddd;
        }

        th {
            background-color: #007bff;
            color: white;
        }

        tr:nth-child(odd) {
            background-color: #ffffff;
            color: black;
        }

        tr:nth-child(even) {
            background-color: black;
            color: #ffffff;
        }

       .delete-btn {
            padding: 5px 10px;
            color: #fff;
            border: none;
            cursor: pointer;
            border-radius: 4px;
            background: #007bff;
        }

       .delete-btn:hover {
            background-color: red;
        }

       .form-popup {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: white;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
            z-index: 10;
        }

       .form-popup input {
            display: block;
            width: 100%;
            margin-bottom: 10px;
            padding: 5px;
        }

       .form-popup button {
            padding: 5px 10px;
            margin-right: 10px;
        }

js

 document.addEventListener('DOMContentLoaded', function () {
            // 获取表格元素
            const studentTable = document.querySelector('.student-table');
            const tbody = studentTable.querySelector('tbody');
            const addBtn = document.getElementById('add-btn');
            const addForm = document.getElementById('add-form');
            const submitAdd = document.getElementById('submit-add');
            const cancelAdd = document.getElementById('cancel-add');

            // 为所有已有的删除按钮添加点击事件处理
            document.querySelectorAll('.delete-btn').forEach(btn => {
                btn.addEventListener('click', function () {
                    if (confirm('确定要删除该学生信息吗?')) {
                        const row = btn.closest('tr');
                        row.remove();
                        updateRowColors();
                    }
                });
            });

            // 显示添加表单
            addBtn.addEventListener('click', function () {
                addForm.style.display = 'block';
            });

            // 取消添加
            cancelAdd.addEventListener('click', function () {
                addForm.style.display = 'none';
            });

            // 提交添加信息
            submitAdd.addEventListener('click', function () {
                const id = document.getElementById('student-id').value;
                const name = document.getElementById('student-name').value;
                const gender = document.getElementById('student-gender').value;
                const college = document.getElementById('student-college').value;
                const classInfo = document.getElementById('student-class').value;
                const major = document.getElementById('student-major').value;
                const counselor = document.getElementById('student-counselor').value;

                if (id && name && gender && college && classInfo && major && counselor) {
                    const newRow = document.createElement('tr');
                    const cells = [id, name, gender, college, classInfo, major, counselor];
                    cells.forEach(cellText => {
                        const cell = document.createElement('td');
                        cell.textContent = cellText;
                        newRow.appendChild(cell);
                    });
                    const deleteCell = document.createElement('td');
                    const deleteBtn = document.createElement('button');
                    deleteBtn.classList.add('delete-btn');
                    deleteBtn.textContent = '删除';
                    deleteBtn.addEventListener('click', function () {
                        if (confirm('确定要删除该学生信息吗?')) {
                            const row = deleteBtn.closest('tr');
                            row.remove();
                            updateRowColors();
                        }
                    });
                    deleteCell.appendChild(deleteBtn);
                    newRow.appendChild(deleteCell);
                    tbody.appendChild(newRow);

                    addForm.style.display = 'none';
                    updateRowColors();

                    // 清空表单
                    document.querySelectorAll('student-id').value = '';
                    document.querySelectorAll('student-name').value = '';
                    document.querySelectorAll('student-gender').value = '';
                    document.querySelectorAll('student-college').value = '';
                    document.querySelectorAll('student-class').value = '';
                    document.querySelectorAll('student-major').value = '';
                    document.querySelectorAll('student-counselor').value = '';
                } else {
                    alert('请填写所有信息!');
                }
            });

            // 更新表格行的颜色,确保奇偶行颜色交替
            function updateRowColors() {
                const rows = studentTable.querySelectorAll('tbody tr');
                rows.forEach((row, index) => {
                    row.style.backgroundColor = index % 2 === 0 ? '#ffffff' : '#000000';
                    row.style.color = index % 2 === 0 ? '#000000' : '#ffffff';
                });
            }

            // 初始化表格行颜色
            updateRowColors();
        });

js第五题

相关文章:

  • JavaEE-SpringBoot快速入门
  • 【再读】2501.12948/DeepSeek-R1通过强化学习提升大型语言模型(LLMs)的推理能力
  • C++函数指针与回调函数详解:从青铜到王者,一文搞懂`std::function`的降维打击!
  • PVE使用一个物理网卡采用VLAN为管理IP和VM分配网络的问题
  • 【MYSQL】视图
  • 蓝桥杯小白打卡第五天
  • 什么是算法的空间复杂度和时间复杂度,分别怎么衡量。
  • 【故障处理】- 执行命令crsctl query crs xxx一直hang
  • 采用分布式部署deepseek
  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_memalign函数
  • van-field的maxlength属性为空会导致输入框的值被清空。
  • SSML语音合成标记语言开发指南:从基础语法到实战案例解析
  • [250217] x-cmd 发布 v0.5.3:新增 DeepSeek AI 模型支持及飞书/钉钉群机器人 Webhook 管理
  • windows 设置poppler
  • unordered_set 和 unordered_map的模拟实现(c++)
  • 【Go入门篇】第一章:从 Java/Python 开发者的视角入门go语言
  • 半导体制造中的“魔法盾牌”:二氧化硅
  • 前端知识速记--HTML篇:HTML5的新特性
  • vLLM专题(六)-Pooling模型
  • floodfill算法系列一>扫雷游戏
  • 工信部:加快自动驾驶系统安全要求强制性国家标准研制
  • 新剧|反谍大剧《绝密较量》央一开播,张鲁一高圆圆主演
  • 多地征集农村假冒伪劣食品违法线索,全链条整治“三无”产品
  • 因高颜值走红的女通缉犯出狱后当主播自称“改邪归正”,账号已被封
  • 伊朗最大港口爆炸:26公里外都能听到,超七百人受伤,原因指向化学品储存
  • 加拿大温哥华一车辆冲撞人群,造成多人伤亡