207 lines
5.4 KiB
HTML
207 lines
5.4 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta charset="utf-8" />
|
||
|
<meta name="author" content="flykhan" />
|
||
|
<meta name="keywords" content="c c++ 物联网" />
|
||
|
<title>DOM操作2</title>
|
||
|
<style>
|
||
|
body {
|
||
|
padding: 10px;
|
||
|
}
|
||
|
|
||
|
.action {
|
||
|
display: inline-block;
|
||
|
width: 30px;
|
||
|
height: 30px;
|
||
|
text-align: center;
|
||
|
line-height: 30px;
|
||
|
border: 1px solid lightgray;
|
||
|
border-radius: 50%;
|
||
|
cursor: pointer;
|
||
|
user-select: none;
|
||
|
}
|
||
|
|
||
|
.action:hover {
|
||
|
background-color: lightgray;
|
||
|
}
|
||
|
|
||
|
.action:active {
|
||
|
background-color: lightskyblue;
|
||
|
}
|
||
|
|
||
|
.warning {
|
||
|
background-color: red;
|
||
|
color: white;
|
||
|
}
|
||
|
|
||
|
.success {
|
||
|
background-color: green;
|
||
|
color: white;
|
||
|
}
|
||
|
|
||
|
.xe_group {
|
||
|
display: flex; /* 弹性布局 */
|
||
|
flex-direction: row; /* 水平方向排列 */
|
||
|
justify-content: center; /* 水平方向居中 */
|
||
|
}
|
||
|
|
||
|
form {
|
||
|
margin: 10px 0;
|
||
|
}
|
||
|
|
||
|
input[name] {
|
||
|
outline: none;
|
||
|
border: 1px solid lightgray;
|
||
|
padding: 5px;
|
||
|
}
|
||
|
</style>
|
||
|
</head>
|
||
|
|
||
|
<body>
|
||
|
<form onsubmit="return addData(this);">
|
||
|
<input type="hidden" name="uid" value="0" />
|
||
|
<input name="username" />
|
||
|
<input name="phone" />
|
||
|
<button type="submit">提交</button>
|
||
|
</form>
|
||
|
|
||
|
<table border="1px" cellspacing="0" cellpadding="10px">
|
||
|
<thead>
|
||
|
<th>ID</th>
|
||
|
<th width="300px">Name</th>
|
||
|
<th>Phone</th>
|
||
|
<th width="300px">Action</th>
|
||
|
</thead>
|
||
|
<tbody>
|
||
|
<tr>
|
||
|
<td>1</td>
|
||
|
<td>flykhan</td>
|
||
|
<td>13536480678</td>
|
||
|
<td class="xe_group">
|
||
|
<b class="action warning">X</b>
|
||
|
<b class="action success">E</b>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td>2</td>
|
||
|
<td>flykhan</td>
|
||
|
<td>13536480678</td>
|
||
|
<td class="xe_group">
|
||
|
<b class="action warning">X</b>
|
||
|
<b class="action success">E</b>
|
||
|
</td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td>3</td>
|
||
|
<td>flykhan</td>
|
||
|
<td>13536480678</td>
|
||
|
<td class="xe_group">
|
||
|
<b class="action warning">X</b>
|
||
|
<b class="action success">E</b>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</table>
|
||
|
|
||
|
<script>
|
||
|
var max_id = 4;
|
||
|
function delAction() {
|
||
|
let ds = document.getElementsByClassName("warning");
|
||
|
for (let i = 0; i < ds.length; i++) {
|
||
|
let bElement = ds[i];
|
||
|
bElement.addEventListener("click", (event) => {
|
||
|
let b = event.target;
|
||
|
let tr = b.parentElement.parentElement;
|
||
|
tr.remove();
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function editAction() {
|
||
|
let ds = document.getElementsByClassName("success");
|
||
|
for (let i = 0; i < ds.length; i++) {
|
||
|
let bElement = ds[i];
|
||
|
bElement.addEventListener("click", (event) => {
|
||
|
let b = event.target;
|
||
|
let tr = b.parentElement.parentElement;
|
||
|
let tds = tr.children; // 获取 tr 行的所有子标签对象
|
||
|
|
||
|
let form = document.forms[0]; // 获取当前页面的第一个表单
|
||
|
|
||
|
form.uid.value = tds[0].innerText;
|
||
|
form.username.value = tds[1].innerText;
|
||
|
form.phone.value = tds[2].innerText;
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
delAction();
|
||
|
editAction();
|
||
|
|
||
|
function addData(form) {
|
||
|
let id_ = form.uid.value;
|
||
|
let name = form.username.value;
|
||
|
let phone = form.phone.value;
|
||
|
|
||
|
console.log(id_);
|
||
|
|
||
|
if (id_ === "0") {
|
||
|
// 将数据添加到 table 中
|
||
|
let table = document.getElementsByTagName("table")[0];
|
||
|
|
||
|
// 创建一个新的 tr 标签
|
||
|
let tr = document.createElement("tr");
|
||
|
// 创建一个新的 td 标签
|
||
|
let td1 = document.createElement("td");
|
||
|
td1.innerText = max_id;
|
||
|
let td2 = document.createElement("td");
|
||
|
td2.innerText = name;
|
||
|
let td3 = document.createElement("td");
|
||
|
td3.innerText = phone;
|
||
|
let td4 = document.createElement("td");
|
||
|
td4.className = "xe_group";
|
||
|
let b1 = document.createElement("b");
|
||
|
b1.className = "action warning";
|
||
|
b1.innerText = "X";
|
||
|
let b2 = document.createElement("b");
|
||
|
b2.className = "action success";
|
||
|
b2.innerText = "E";
|
||
|
|
||
|
// 将 td1, td2, td3, td4, b1, b2 添加到 tr 中
|
||
|
tr.append(td1);
|
||
|
tr.append(td2);
|
||
|
tr.append(td3);
|
||
|
tr.append(td4);
|
||
|
td4.append(b1);
|
||
|
td4.append(b2);
|
||
|
|
||
|
// 将 tr 添加到 table 中
|
||
|
table.append(tr);
|
||
|
|
||
|
max_id++;
|
||
|
delAction(); // 添加方法监听
|
||
|
editAction();
|
||
|
} else {
|
||
|
console.log("---modify---");
|
||
|
let trs = document.getElementsByTagName("tr");
|
||
|
for (let i = 0; i < trs.length; i++) {
|
||
|
if (trs[i].children[0].innerText == id_) {
|
||
|
console.log(trs[i]);
|
||
|
trs[i].children[1].innerText = name;
|
||
|
trs[i].children[2].innerText = phone;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
form.uid.value = "0";
|
||
|
form.username.value = "";
|
||
|
form.phone.value = "";
|
||
|
|
||
|
return false; // 阻止表单的默认提交行为, 不会向服务器提交数据, 只是前端的数据处理
|
||
|
}
|
||
|
</script>
|
||
|
</body>
|
||
|
</html>
|