<!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>Document</title>
<style>
table {
width: 500px;
margin: 100px auto;
border-collapse: collapse;
text-align: center;
}
td,
th {
border: 1px solid #333;
}
thead tr {
height: 40px;
background-color: #ccc;
}
</style>
</head>
<body>
<table cellspacing="0">
<thead>
<tr>
<th>姓名</th>
<th>科目</th>
<th>成绩</th>
<th>操作</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
// 准备数据
var datas = [{
name: ‘魏璎珞‘,
subject: ‘3‘,
score: 100
}, {
name: ‘李四‘,
subject: ‘2‘,
score: 80
}, {
name: ‘李玉‘,
subject: ‘1‘,
score: 78
}, {
name: ‘魏恶习‘,
subject: ‘4‘,
score: 90
}];
// 1 创建行
var tbody = document.querySelector(‘tbody‘);
for (var i = 0; i < datas.length; i++) {
// 2 创建tr行
var tr = document.createElement(‘tr‘);
tbody.appendChild(tr);
// 创建单元格
for (var k in datas[i]) {
// 创建单元格
var td = document.createElement(‘td‘);
td.innerHTML = datas[i][k];
tr.appendChild(td);
}
// 3 创建 删除 单元格
var td = document.createElement(‘td‘);
td.innerHTML = "<a href=‘javascript:;‘>删除</a>";
tr.appendChild(td);
}
// 4 删除单元格
var as = document.querySelectorAll(‘a‘);
// console.log(trs);
for (var i = 0; i < as.length; i++) {
as[i].onclick = function() {
// 点击a 删除所在的行
tbody.removeChild(this.parentNode.parentNode);
}
}
</script>
</body>
</html>