动态表格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>

视频演示:

动态表格

相关推荐
刺客-Andy9 分钟前
React 第七十节 Router中matchRoutes的使用详解及注意事项
前端·javascript·react.js
前端工作日常24 分钟前
我对eslint的进一步学习
前端·eslint
禁止摆烂_才浅1 小时前
VsCode 概览尺、装订线、代码块高亮设置
前端·visual studio code
程序员猫哥1 小时前
vue跳转页面的几种方法(推荐)
前端
代码老y2 小时前
十年回望:Vue 与 React 的设计哲学、演进轨迹与生态博弈
前端·vue.js·react.js
一条上岸小咸鱼2 小时前
Kotlin 基本数据类型(五):Array
android·前端·kotlin
zzywxc7872 小时前
详细探讨AI在金融、医疗、教育和制造业四大领域的具体落地案例,并通过代码、流程图、Prompt示例和图表等方式展示这些应用的实际效果。
开发语言·javascript·人工智能·深度学习·金融·prompt·流程图
大明882 小时前
用 mouseover/mouseout 事件代理模拟 mouseenter/mouseleave
前端·javascript
小杨梅君2 小时前
vue3+vite中使用自定义element-plus主题配置
前端·element
一个专注api接口开发的小白2 小时前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
前端·数据挖掘·api