动态表格html

题目:

要求:

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

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

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

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

要点:(原理)

  1. 通过.value获取表单内容,再用innerHTML和添加节点,新增到表单上。

  2. 通过获取删除按钮,绑定事件,进行remove删除节点,不要忘记把新增的内容也获取过来

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 0; } /* a{ color: rgb(247, 4, 4); display: inline-block; } */ table { border-collapse: collapse; width: 800px; margin: 0 auto; border-spacing: 20px; text-align: center; padding: 10px; } .add { cursor: pointer; } a { color: pink; display: inline-block; } tbody tr:nth-child(odd) { background-color: black; color: white; } tbody tr:nth-child(even) { background-color: white; color: black; } thead tr:nth-child(1) { background-color: greenyellow; color: black; } tr { height: 40px; } .popup { display: none; background-color: whitesmoke; border-radius: 10px; width: 300px; height: 350px; position: absolute; top: 0px; left: 40%; }
    复制代码
     </style>
    </head> <body>
    学号 姓名 性别 二级学院 班级 专业 辅导员 添加
    24250101 小蓝 计算机专业 一班 物联网专业 魔仙女王 删除
    24250102 游乐王子 计算机专业 三班 软件工程 魔仙女王 删除
    24250103 玲珑 计算机专业 二班 软件工程 魔仙女王 删除
    24250104 严莉莉 计算机专业 三班 软件工程 魔仙女王 删除
    24250105 美琪 计算机专业 三班 物联网工程 魔仙女王 删除
    24250106 美雪 计算机专业 三班 物联网工程 魔仙女王 删除
    24250107 小月 计算机专业 三班 软件工程 魔仙女王 删除
    24250108 石小龙 计算机专业 三班 软件工程 学校老师 删除
    24250109 方珍妮 计算机专业 三班 软件工程 学校老师 删除
    24250110 魔仙小千 计算机专业 三班 软件工程 魔仙女王 删除
    <form id="addForm"> <label for="studentId">学号:</label>

    <label for="name">姓名:</label>

    <label for="gender">性别:</label> <select id="gender" name="gender"> <option value="男">男</option> <option value="女">女</option> </select>

    <label for="college">二级学院:</label>

    <label for="class">班级:</label>

    <label for="major">专业:</label>

    <label for="counselor">辅导员:</label>

    </form>
    </body> <script> const add = document.querySelector('.add') const popUp = document.querySelector('.popup') const addForm = document.querySelector('#addForm') add.addEventListener('click',function() { popUp.style.display = 'block'; }) addForm.addEventListener('submit',function(e) { e.preventDefault() const studentId = document.querySelector('#studentId').value const name = document.querySelector('#name').value const gender = document.querySelector('#gender').value const college = document.querySelector('#college').value const Class = document.querySelector('#class').value const major = document.querySelector('#major').value const counselor = document.querySelector('#counselor').value const tableBody = document.querySelector('tbody') const newText = document.createElement('tr') newText.innerHTML = ` ${studentId} ${name} ${gender} ${college} ${Class} ${major} ${counselor} 删除 ` popUp.style.display = 'none'; tableBody.appendChild(newText) const dels = document.querySelectorAll('.del') dels.forEach(function(button){ button.addEventListener('click',function(e) { e.preventDefault() if(confirm('你确定要删除吗?')) { let All = this.parentNode.parentNode All.parentNode.removeChild(All) } }) }) }) </script> </html>

视频演示:

动态表格

相关推荐
恋猫de小郭8 小时前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
崔庆才丨静觅14 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby606115 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了15 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅15 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅16 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅16 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment16 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅16 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊17 小时前
jwt介绍
前端