-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path前端-第4周第2次-JS(3)-2022·2·11.html
133 lines (119 loc) · 3.78 KB
/
前端-第4周第2次-JS(3)-2022·2·11.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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>前端-第4周第2次-JS(3)-2022·2·11</title>
<style type="text/css">
table {
border-collapse: collapse;
margin: 100px auto;
}
th, td {
border: 1px solid #999;
padding: 5px 10px;
}
td:last-child {
cursor: pointer;
}
a {
text-decoration: none;
color: red;
}
caption {
font-weight: bold;
font-size: 30px;
}
form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 0px auto;
}
form > div {
display: flex;
justify-content: center;
align-items: center;
padding: 5px;
}
</style>
</head>
<body>
<table>
<caption>学生信息</caption>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>学号</th>
<th>电话</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<tr>
<td>Li Ming</td>
<td>22</td>
<td>20200001</td>
<td>13423554322</td>
<td>liming@163.com</td>
<td>删除</td>
</tr>
</table>
<form name="form" autocomplete="off">
<h3>提交学生信息</h3>
<div>
<label for="name">姓名</label>
<input type="text" name="name" id="name" required value="Chen">
</div>
<div>
<label for="age">年龄</label>
<input type="number" name="age" id="age" required value="20">
</div>
<div>
<label for="id">学号</label>
<input type="number" name="id" id="id" required value="2344">
</div>
<div>
<label for="phone">电话</label>
<input type="tel" name="phone" id="phone" required value="12343234338">
</div>
<div>
<label for="email">邮箱</label>
<input type="email" name="email" id="email" required value="suy@don.com">
</div>
<div>
<button name="add" type="button" id="add">添加</button>
</div>
</form>
<script type="text/javascript">
window.onload = () => {
document.getElementById('add').onclick = handleAdd;
let trs = document.getElementsByTagName('tr');
for(let i = 0; i < trs.length; ++i) {
trs[i].lastElementChild.onclick = handleDelete;
}
};
function handleDelete() {
this.parentNode.remove();
}
function handleAdd() {
const td = document.createElement('td');
const tr = document.createElement('tr');
const name = td.cloneNode();
name.innerHTML = document.getElementById('name').value;
const age = td.cloneNode();
age.innerHTML = document.getElementById('age').value;
const id = td.cloneNode();
id.innerHTML = document.getElementById('id').value;
const phone = td.cloneNode();
phone.innerHTML = document.getElementById('phone').value;
const email = td.cloneNode();
email.innerHTML = document.getElementById('email').value;
const del = td.cloneNode();
del.innerHTML = '删除';
del.onclick = handleDelete;
tr.append(name, age, id, phone, email, del);
document.getElementsByTagName('table')[0].append(tr);
}
</script>
</body>
</html>