DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />//引入css框架
head>
<style type="text/css">
.button-trash{//为class="table"的class标签设置css格式
display: inline-block;
border: 0;
width: 2rem;
height: 1rem;
outline: 0;
background-image:url(
"data:image/svg+xml,"
);
background-repeat: no-repeat;
background-size: 1rem 1rem;
background-color: transparent;
}
.button-trash:focus {//为class="table"的class标签设置鼠标移入时的css格式
border: 0;
outline: 0;
}
style>
<body>
<table class="table">//表格table
<thead>//表头
<tr>//表头内容
<th scope="col">学号th>//具体每列的表头内容
<th scope="col">姓名th>
<th scope="col">班级th>
<th scope="col">性别th>
<th scope="col">操作th>
tr>
thead>
<tbody id="tb">//表格主体,用来存放具体数据,其内容用JS代码动态生成
tbody>
table>
<script type="text/javascript">
window.onload = function(){
var students = [
{
"studentNum": "201711104043",
"name": "马阳",
"class": "软件182",
"gender": "男"
},
{
"studentNum": "201804102067",
"name": "夏一鸣",
"class": "软件181",
"gender": "男"
},
{
"studentNum": "201810101153",
"name": "李志勇",
"class": "软件182",
"gender": "男"
},
{
"studentNum": "201811104001",
"name": "姜霞",
"class": "软件181",
"gender": "女"
},
{
"studentNum": "201811104002",
"name": "齐永智",
"class": "软件181",
"gender": "女"
},
{
"studentNum": "201811104003",
"name": "王雯雯",
"class": "软件181",
"gender": "女"
},
{
"studentNum": "201811104004",
"name": "高小菲",
"class": "软件181",
"gender": "女"
},
{
"studentNum": "201811104005",
"name": "赵蕊蕊",
"class": "软件181",
"gender": "女"
},
{
"studentNum": "201811104006",
"name": "骆家禾",
"class": "软件181",
"gender": "男"
},
{
"studentNum": "201811104007",
"name": "王飞",
"class": "软件181",
"gender": "男"
},
{
"studentNum": "201811104008",
"name": "倪恒智",
"class": "软件181",
"gender": "男"
},
{
"studentNum": "201811104009",
"name": "李泽凯",
"class": "软件181",
"gender": "男"
},
{
"studentNum": "201811104010",
"name": "马龙",
"class": "软件181",
"gender": "男"
},
{
"studentNum": "201811104011",
"name": "王筝",
"class": "软件181",
"gender": "男"
},
{
"studentNum": "201811104012",
"name": "王梓一",
"class": "软件181",
"gender": "男"
}
];
var tableRow, cellNum, cellName, cellClass, cellGender, cellOperation, btnDelete;
var studentsTbody = document.getElementById("tb");
for(i = 0; i < students.length; i++) {
var student = students[i];
tableRow = document.createElement('tr');
studentsTbody.appendChild(tableRow);
cellNum = document.createElement('td');
cellNum.textContent = student.studentNum;
tableRow.appendChild(cellNum);
cellName = document.createElement('td');
cellName.textContent = student.name;
tableRow.appendChild(cellName);
cellClass = document.createElement('td');
cellClass.textContent = student.class;
tableRow.appendChild(cellClass);
cellGender = document.createElement('td');
cellGender.textContent = student.gender;
tableRow.appendChild(cellGender);
cellOperation = document.createElement('td');
btnDelete = document.createElement('button');
btnDelete.className = 'button-trash';
cellOperation.appendChild(btnDelete);
tableRow.appendChild(cellOperation);
btnDelete.addEventListener('click', function() {
confirm(`确定要删除学生${this.parentElement.parentElement.children[1].innerText}(${this.parentElement.parentElement.children[0].innerText})吗?`);
});
}
}
script>
body>
html>
- 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
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173