[JavaScript] 纯文本查看 复制代码 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生就业登记表</title>
<style>
body {
background-color: #fff;
}
.wrapper {
width: 700px;
height: 400px;
margin: 100px auto;
}
.inner {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 40px;
}
.p-1 {
width: 50px;
border-radius: 2px;
}
.info {
display: flex;
justify-content: space-around;
margin-top: 10px;
width: 100%;
height: 40px;
}
.btn {
width: 50px;
height: 25px;
border: none;
color: #fff;
cursor: pointer;
background-color: #143875;
border-radius: 2px;
}
select {
border-radius: 2px;
}
.statistics {
position: relative;
display: flex;
align-items: center;
width: 100%;
height: 40px;
background-color: #F2F2F2;
}
.num {
font-weight: bold;
color: fuchsia;
}
.statistics_wrap {
position: absolute;
right: 10px;
letter-spacing: 2px;
font-size: 14px;
}
.list_wrap {
width: 100%;
margin-top: 10px;
border-collapse: collapse;
}
.list_wrap tr {
height: 40px;
background-color: #F2F2F2;
}
.list_wrap th {
border-collapse: collapse;
}
.t_tbody tr {
font-size: 14px;
text-align: center;
}
.t_tbody td {
border-collapse: collapse;
}
.t_tbody a {
background-color: crimson;
display: block;
border: none;
border-radius: 2px;
text-decoration: none;
color: #fff;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="inner">
<h3 class="title">学生就业登记表</h3>
</div>
<form class="info" autocapitalize="off">
<label>姓名:<input type="text" class="p-1" name="uname"></label>
<label>年龄:<input type="number" class="p-1" name="age"></label>
<label>薪资:<input type="number" class="p-1" name="salary"></label>
<label>
性别:
<select name="gender">
<option value="男">男</option>
<option value="女">女</option>
</select>
</label>
<label>
就业城市:
<select name="city">
<option value="乌鲁木齐">乌鲁木齐</option>
<option value="吐鲁番">吐鲁番</option>
<option value="塔城">塔城</option>
<option value="伊犁">伊犁</option>
</select>
</label>
<button class="btn">添加</button>
</form>
<div class="statistics">
<div class="statistics_wrap">共有数据<span class="num">0</span>条</div>
</div>
<table class="list_wrap">
<thead class="list">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>薪资</th>
<th>就业城市</th>
<th>录入时间</th>
<th>操作</th>
</tr>
</thead>
<tbody class="t_tbody">
</tbody>
</table>
</div>
<script>
const intDate = [{
stuId: 1,
uname: '法外狂徒张三',
age: 20,
gender: '男',
salary: '9.9',
city: '各地',
time: '2023/12/11 13:18:00'
}]
// 原数组对象以字符串的方式保存到本地存储
//TODO:
// 注意事项:第一次启动之后注释下面一行代码。
// 注释方法:在代码最前端写//符号
// 例子: //localStorage.setItem('data', JSON.stringify(intDate))
localStorage.setItem('data', JSON.stringify(intDate))
// 以转换对象的方式读取原字符串数据
const arr = JSON.parse(localStorage.getItem('data')) || []
console.log(arr)
const t_tbody = document.querySelector('.t_tbody')
const num = document.querySelector('.statistics_wrap .num')
const info = document.querySelector('.info')
const p_1 = document.querySelectorAll('.info .p-1')
const uname = document.querySelector('[name=uname]')
const age = document.querySelector('[name=age]')
const salary = document.querySelector('[name=salary]')
const gender = document.querySelector('[name=gender]')
const city = document.querySelector('[name=city]')
function render() {
const trArr = arr.map(function (ele, index) {
return `
<tr>
<td>${ele.stuId}</td>
<td>${ele.uname}</td>
<td>${ele.age}岁</td>
<td>${ele.gender}</td>
<td>${ele.salary}元</td>
<td>${ele.city}</td>
<td>${ele.time}</td>
<td>
<a href="javascript:" data-id="${index}">删除</a>
</td>
</tr>
`
})
t_tbody.innerHTML = trArr.join('')
num.innerHTML = arr.length
}
render()
info.addEventListener('submit', function (e) {
e.preventDefault()
for (let i = 0; i < p_1.length; i++) {
if (!p_1.value) {
return alert('用户输入不能为空!')
}
}
arr.push({
stuId: arr.length ? arr[arr.length - 1].stuId + 1 : 1,
uname: uname.value,
age: age.value,
gender: gender.value,
salary: salary.value,
city: city.value,
time: new Date().toLocaleString()
})
render()
this.reset()
localStorage.setItem('data', JSON.stringify(arr))
})
t_tbody.addEventListener('click', function (e) {
if (e.target.tagName === 'A') {
if (confirm('您确定要删除本条数据吗?')) {
arr.splice(e.target.dataset.id, 1)
render()
localStorage.setItem('data', JSON.stringify(arr))
}
}
})
</script>
</body>
</html> |