1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| <!DOCTYPE html> <html lang="zh">
<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 type="text/css"> * { padding: 0; margin: 0; box-sizing: border-box; }
a { text-decoration: none; }
.demo { width: 600px; height: 300px; margin: 100px auto; }
table { width: 400px; text-align: center; border-collapse: collapse; line-height: 40px; }
table th, table td { border: 1px solid #222; width: 100px;
}
thead { background-color: #ccc; } </style> </head>
<body> <div class="demo"> <table> <thead> <th>姓名</th> <th>科目</th> <th>成绩</th> <th>操作</th> </thead> <tbody> </tbody> </table> </div>
<script> var data = [ { username: '张三', kemu: 'JavaScript', chengji: 100, }, { username: '李四', kemu: 'JavaScript', chengji: 90, }, { username: '王五', kemu: 'JavaScript', chengji: 80, }, ] var tb = document.querySelector('tbody'); for (var i = 0; i < data.length; i++){ var tr = document.createElement('tr'); tb.appendChild(tr);
for (var k in data[i]) { var td = document.createElement('td'); td.innerHTML = data[i][k]; tr.appendChild(td) } var td = document.createElement('td'); td.innerHTML = "<a href='javascript:;'>删除</a>" tr.appendChild(td); } var as = document.querySelectorAll('a') for (var i = 0; i < as.length; i++) { as[i].onclick = function () { tb.removeChild(this.parentNode.parentNode); } } </script> </html>
|