if (e.target.tagName === 'A') {
if (window.confirm('确认删除!')) {
const id = e.target.dataset.id
arr.splice(id, 1)
render() (为什么要调用这个函数?)
}
}
})
```javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>学生信息管理</title>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #721c24;
}
h1 {
text-align: center;
color: #333;
margin: 20px 0;
}
table {
margin: 0 auto;
width: 800px;
border-collapse: collapse;
color: #004085;
}
th {
padding: 10px;
background: #cfe5ff;
font-size: 20px;
font-weight: 400;
}
td,
th {
border: 1px solid #b8daff;
}
td {
padding: 10px;
color: #666;
text-align: center;
font-size: 16px;
}
tbody tr {
background: #fff;
}
tbody tr:hover {
background: #e1ecf8;
}
.info {
width: 900px;
margin: 50px auto;
text-align: center;
}
.info input,
.info select {
width: 80px;
height: 27px;
outline: none;
border-radius: 5px;
border: 1px solid #b8daff;
padding-left: 5px;
box-sizing: border-box;
margin-right: 15px;
}
.info button {
width: 60px;
height: 27px;
background-color: #004085;
outline: none;
border: 0;
color: #fff;
cursor: pointer;
border-radius: 5px;
}
.info .age {
width: 50px;
}
</style>
</head>
<body>
<h1>新增学员</h1>
<form class="info" autocomplete="off">
姓名:<input type="text" class="uname" name="uname" />
年龄:<input type="text" class="age" name="age" />
性别:
<select name="gender" class="gender">
<option value="男">男</option>
<option value="女">女</option>
</select>
薪资:<input type="text" class="salary" name="salary" />
就业城市:<select name="city" class="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广州">广州</option>
<option value="深圳">深圳</option>
<option value="曹县">曹县</option>
</select>
<button class="add">录入</button>
</form>
<h1>就业榜</h1>
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>薪资</th>
<th>就业城市</th>
<th>操作</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
// 添加一个空数组,保存用户输入的数据
const arr = []
// 获取到表单标签 给表单标签注册 submit 事件:当提交表单时触发
const info = document.querySelector('.info')
info.addEventListener('submit',function(e){
// 阻止默认事件发生,当表单提交时会跳转页面
e.preventDefault()
// 获取对应表单里面的value值 即输入的数据 trim()清除文本内容左右两边的空格,只能清除左右两边
const uname = document.querySelector('.uname').value.trim() //用户名
const age = document.querySelector('.age').value.trim() //年龄
const gender = document.querySelector('.gender').value //性别
const salary = document.querySelector('.salary').value.trim() //收入
const city = document.querySelector('.city').value //住址
// 判断,如果输入的内容为空代码就不往下继续执行,且提示用户输入数据
if (uname.lenght === 0 || age.lenght === 0 || salary.lenght === 0) {
return alert('请输入数据!')
}
// 如果继续执行就代表用户输入了完整的数据
// 新建一个对象,保存用户输入的一个信息
// es6 高级语法,属性和属性名相同则只用写一个
// console.log(arr.length + 1)
const obj = {
// id数值是和数组长度一致,由于数组最开始为空数组所以长度为0因此要加一
id:arr.length + 1,
uname,
age,
gender,
salary,
city
}
// 将录入的对象添加到数组里
arr.push(obj)
// 渲染数据
render()
// 清空输入框表格;reset()清空表格方法
this.reset()
})
// 根据数组,将数组里面的数据渲染显示到表格中
function render() {
// 将tbody里面的内容清空
document.querySelector('tbody').innerHTML = ''
// 遍历数组
for (let i = 0; i < arr.length; i++) {
// 创建tr标签
const tr = document.createElement('tr')
// 给tr标签里面写入对应的td
tr.innerHTML = `
<td>${arr[i].id}</td>
<td>${arr[i].uname}</td>
<td>${arr[i].age}</td>
<td>${arr[i].gender}</td>
<td>${arr[i].salary}</td>
<td>${arr[i].city}</td>
<td>
<a href="javascript:" data-id="${i}">删除</a>
</td>
`
// 给tbody添加tr元素节点
document.querySelector('tbody').appendChild(tr)
}
}
// 获取tr标签;;新增的元素想要绑定事件就只能用事件委托 tbody为父元素
document.querySelector('tbody').addEventListener('click',function(e){
// 确定触发事件的对象是a标签
if (e.target.tagName === 'A') {
// 判断是否真的删除此元素
if (window.confirm('确认删除!')) {
// 如何确认删除的是我们要删除的元素?利用自定义属性,给元素添加一个id
const id = e.target.dataset.id
// 在数组里将此元素删除;
arr.splice(id,1)
// 再次渲染表格数据,使页面显示删除元素后的表格
render()
}
}
})
</script>
</body>
</html>
```
render函数是一个自定义的函数,它的作用就是 当你删除 或者 新增 某条数据之后,重新渲染一下当前页面上的数据