动态生成表格,且可以删除(刷新后复位)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
table{
margin:0 auto;
}
td,th{
background-color: gray;
color: white;
width: 100px;
height: 30px;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>姓名</th>
<th>学科</th>
<th>成绩</th>
<th>操作</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>
var datas =[
{
name: 'zyf',
subject: 'computer',
score: 99
},
{
name: 'my',
subject: 'art',
score: 100
},
{
name: 't',
subject: 'sports',
score: 70
}];
var tbody = document.querySelector('tbody');
for(var i=0;i<datas.length;i++){
var tr = document.createElement('tr');
for(var k in datas[i]){
var td = document.createElement('td');
td.innerText = datas[i][k];
tr.appendChild(td);
}
var td = document.createElement('td');
td.innerHTML = '<a href="javascript:;">删除</a>';
tr.appendChild(td);
tbody.appendChild(tr);
}
var as =document.querySelectorAll('a');
for(var i =0;i<as.length;i++){
as[i].onclick = function(){
tbody.removeChild(this.parentNode.parentNode)
}
}
</script>
</body>
</html>