<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="样式.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>新增学员</h1>
<div class="info">
姓名:<input type="text" class="uname"> 年龄:
<input type="text" class="age"> 性别:
<select name="gender" class="gender">
<option value="男">男</option>
<option value="女">女</option>
</select> 薪资:
<input type="text" class="salary"> 就业城市:
<select name="city" id="" class="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广州">广州</option>
<option value="深圳">深圳</option>
</select>
<button class="add" onclick="addStudent(this.parentNode)">录入</button>
</div>
<h1>就业榜</h1>
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>薪资</th>
<th>就业城市</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td>1001</td>
<td>刘备</td>
<td>19</td>
<td>男</td>
<td>15000</td>
<td>上海</td>
<td>
<a href="#" onclick="deleteStudent(this)">删除</a>
</td>
</tr>
</tbody>
</table>
<script>
let arr = [{
stuId: 1001,
uname: '刘备',
age: 19,
gender: '男',
salary: '20000',
city: '上海'
}, {
stuId: 1002,
uname: '关羽',
age: 29,
gender: '男',
salary: '30000',
city: '北京'
}, {
stuId: 1003,
uname: '张飞',
age: 39,
gender: '男',
salary: '2000',
city: '北京'
}];
var tbody = document.querySelector("tbody");
var id = 1004;
reFlash();
function reFlash(){
tbody.innerHTML = "";
for (let itme in arr){
var tr = document.createElement("tr");
for (let val in arr[itme]){
let td = document.createElement("td");
td.innerText = arr[itme][val];
tr.append(td);
}
tr.append(deletebtn());
tbody.append(tr);
}
}
function deleteStudent(dom){
tbody.removeChild(dom.parentNode.parentNode);
}
function addStudent(form){
let tr = document.createElement("tr");
var chileds = form.children;
let tdID = document.createElement("td");
var newStu = {
stuId: id,
uname: '',
age: 0,
gender: '',
salary: '',
city: ''
};
tdID.innerHTML = id++;
tr.append(tdID);
for(let i=0; i < 5; i++){
let td = document.createElement("td");
td.innerHTML = chileds[i].value;
newStu[chileds[i].className] = chileds[i].value;
tr.append(td);
}
tr.append(deletebtn(tr));
arr.push(newStu);
tbody.append(tr);
}
function deletebtn(index){
let a = document.createElement("a");
a.href = "#";
a.id = index;
a.onclick = function (){
arr.splice(this.id,1);
deleteStudent(this);
}
a.innerHTML = "删除";
let td = document.createElement("td");
td.append(a);
return td;
}
</script>
</body>
</html>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color:#721c24;
}
h1 {
text-align: center;
color:#333;
margin: 20px 0;
}
table {
margin:0 auto;
width: 800px;
border-collapse: collapse;
color:#004085;
}
th {
padding: 10px;
background: #cfe5ff;
font-size: 20px;
font-weight: 400;
}
td,th {
border:1px solid #b8daff;
}
td {
padding:10px;
color:#666;
text-align: center;
font-size: 16px;
}
tbody tr {
background: #fff;
}
tbody tr:hover {
background: #e1ecf8;
}
.info {
width: 900px;
margin: 50px auto;
text-align: center;
}
.info input {
width: 80px;
height: 25px;
outline: none;
border-radius: 5px;
border:1px solid #b8daff;
padding-left: 5px;
}
.info button {
width: 60px;
height: 25px;
background-color: #004085;
outline: none;
border: 0;
color: #fff;
cursor: pointer;
border-radius: 5px;
}
.info .age {
width: 50px;
}
