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

视频演示:

动态表格

相关推荐
独孤求败Ace1 小时前
第40天:Web开发-JS应用&VueJS框架&Vite构建&启动打包&渲染XSS&源码泄露&代码审计
前端·javascript·vue.js
天若子1 小时前
flutter ListView Item复用源码解析
前端·flutter
文城5214 小时前
HTML-day1(学习自用)
前端·学习·html
阿珊和她的猫4 小时前
Vue 和 React 的生态系统有哪些主要区别
前端·vue.js·react.js
偷光4 小时前
深度剖析 React 的 useReducer Hook:从基础到高级用法
前端·javascript·react.js
Thomas_YXQ5 小时前
Unity3D Shader 简析:变体与缓存详解
开发语言·前端·缓存·unity3d·shader
2201_756942646 小时前
react入门笔记
javascript·笔记·react.js
傻小胖6 小时前
ES6 Proxy 用法总结以及 Object.defineProperty用法区别
前端·javascript·es6
Web极客码6 小时前
如何跟踪你WordPress网站的SEO变化
前端·搜索引擎·wordpress