点击删除链接,删除本行
<!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 {
border-collapse: collapse;
/* 相邻边被合并 */
/* text-decoration: none; */
text-align: center;
position: absolute;
}
thead{
background-color: rgb(35, 35, 136);
}
</style>
</head>
<body>
<table border="1">
<thead>
<th>姓名</th>
<th>科目</th>
<th>成绩</th>
<th>操作</th>
</thead>
<tbody>
</tbody>
</table>
<script>
var dates = [{
name: '刘雨昕',
subject: 'JavaScript',
score: 100
}, {
name: 'lyx',
subject: 'JavaScript',
score: 99
}, {
name: 'liuyuxin',
subject: 'JavaScript',
score: 56
}, {
name: '昕xin',
subject: 'JavaScript',
score: 0
},];
var tbody = document.querySelector('tbody');
for (var i = 0; i < dates.length; i++) {
var tr = document.createElement('tr');
tbody.appendChild(tr);
for (var k in dates[i]) {
var td = document.createElement('td');
tr.appendChild(td);
td.innerHTML = dates[i][k];
}
var td_2=document.createElement('td')
td_2.innerHTML='<a href="javascript:;">删除</a>';
tr.appendChild(td_2);
}
var a_arr=document.querySelectorAll('a');
for(var i=0;i<a_arr.length;i++){
a_arr[i].onclick=function(){
tbody.removeChild(this.parentNode.parentNode);
}
}
</script>
</body>
</html>