<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.boss {
width: 40%;
height: 500px;
border: 1px solid #00FFFF;
text-align: center;
float: left;
font-size: 30px;
}
.boss input {
width: 60%;
height: 50px;
font-size: 30px;
}
.box {
width: 50%;
float: right;
text-align: center;
}
.btn {
width: 200px;
height: 40px;
font-size: 20px;
border-radius: 10px;
}
</style>
</head>
<body>
<input type="text" id="sousuo" placeholder="请输入数据">
<button class="sb">搜索</button>
<div class="boss">
<p>姓名:<input type="text" /></p>
<p>年龄:<input type="text" /></p>
<p>性别:<input type="text" /></p>
<p>学号:<input type="text" /></p>
<button class="btn">提交</button>
</div>
<table class="box" border="" cellspacing="" cellpadding="">
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>学号</th>
<th>操作</th>
</tr>
</table>
</body>
<script>
var box = document.querySelector(".box");
var btn = document.querySelector(".btn");
var inp = document.querySelectorAll(".boss input");
var arr = [];
var obj = true;
btn.onclick = function () {
if (!obj) {
console.log("编辑");
var str = {
name: inp[0].value,
age: inp[1].value,
sex: inp[2].value,
id: inp[3].value,
}
// trim()去除首尾空格
for (var i = 0; i < inp.length; i++) {
if (inp[i].value.trim() == "" || inp[i].value.trim() == null || inp[i].value.trim() == undefined) {
alert("信息不完善,请填写完整!");
return false;
}
}
for (var i = 0; i < arr.length; i++) {
if (arr[i].id == str.id) {
arr.splice(i, 1, str);
}
}
var trs = document.querySelectorAll("tr");
for (var i = 1; i < trs.length; i++) {
if (str.id == trs[i].children[4].children[0].value) {
trs[i].children[1].innerHTML = str.name;
trs[i].children[2].innerHTML = str.age;
trs[i].children[3].innerHTML = str.sex;
trs[i].children[4].innerHTML = str.id;
var edits = trs[i].querySelector(".edits");
var saves = trs[i].querySelector(".saves");
saves.style.display = "none";
edits.style.display = "block";
}
}
delNull();
} else {
var str = {
name: inp[0].value,
age: inp[1].value,
sex: inp[2].value,
id: inp[3].value,
}
// trim()去除首尾空格
for (var i = 0; i < inp.length; i++) {
if (inp[i].value.trim() == "" || inp[i].value.trim() == null || inp[i].value.trim() == undefined) {
alert("信息不完善,请填写完整!");
return false;
}
}
if (arr.length > 0) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].id == str.id) {
alert("学号已存在,请重新输入!");
return false;
}
}
arr.push(str);
} else {
arr.push(str);
}
var trs = document.createElement("tr");
box.appendChild(trs);
var xh = document.createElement("td");
xh.innerHTML = arr.length;
trs.appendChild(xh);
for (var i = 0; i < inp.length; i++) {
var tds = document.createElement("td");
trs.appendChild(tds);
tds.innerHTML = inp[i].value;
}
var del = document.createElement("td");
del.innerHTML = "<button οnclick='dels(this)'>删除</button>"
trs.appendChild(del);
var edit = document.createElement("td");
edit.innerHTML = "<button class='edits' οnclick='edits(this)'>编辑</button><button class='saves' style='display: none;' οnclick='save(this)'>保存</button>"
trs.appendChild(edit);
var arrow = document.createElement("td");
arrow.innerHTML = "<button οnclick='up(this)'>向上</button><button οnclick='dw(this)'>向下</button>"
trs.appendChild(arrow);
delNull();
}
};
//向下
function dw(e) {
var tds = document.querySelectorAll("tr");
if (e.parentNode.parentNode.children[0].innerHTML == arr.length) {
alert("是最后一行了")
} else {
for (var i = 1; i <= 4; i++) {
var ten = e.parentNode.parentNode.children[i].innerHTML;
e.parentNode.parentNode.children[i].innerHTML = e.parentNode.parentNode.nextSibling.children[i].innerHTML;
e.parentNode.parentNode.nextSibling.children[i].innerHTML = ten;
}
}
}
//向上
function up(e) {
var tds = document.querySelectorAll("tr");
if (e.parentNode.parentNode.children[0].innerHTML == 1) {
alert("到顶了")
} else {
for (var i = 1; i <= 4; i++) {
var ten = e.parentNode.parentNode.children[i].innerHTML;
e.parentNode.parentNode.children[i].innerHTML = e.parentNode.parentNode.previousSibling.children[i].innerHTML;
e.parentNode.parentNode.previousSibling.children[i].innerHTML = ten;
}
}
}
// 编辑
function edits(e) {
var fuIndex = e.parentNode.parentNode;
for (var i = 1; i < 5; i++) {
var inps = document.createElement("input");
inps.setAttribute("type", "text");
if (i == 4) {
inps.setAttribute("disabled", "true");
}
inps.style.width = "40%";
inps.value = fuIndex.children[i].innerHTML;
var tds = fuIndex.children[i];
fuIndex.children[i].innerHTML = "";
tds.appendChild(inps);
var edits = fuIndex.querySelector(".edits");
edits.style.display = "none";
var saves = fuIndex.querySelector(".saves");
saves.style.display = "block";
}
var inpus = document.querySelectorAll(".boss input");
for (var i = 0; i < inpus.length; i++) {
inpus[i].value = fuIndex.children[i + 1].children[0].value;
inpus[3].setAttribute("disabled", "true");
}
obj = false;
}
// 保存
function save(e) {
var fuIndex = e.parentNode.parentNode;
var input = fuIndex.querySelectorAll("input");
for (var i = 1; i <= input.length; i++) {
fuIndex.children[i].innerHTML = input[i - 1].value;
input[i - 1].remove();
}
for (var i = 0; i < arr.length; i++) {
if (arr[i].id == input[3].value) {
arr[i].name = input[0].value;
arr[i].age = input[1].value;
arr[i].sex = input[2].value;
arr[i].id = input[3].value;
}
}
console.log(arr);
var edits = fuIndex.querySelector(".edits");
edits.style.display = "none";
var saves = fuIndex.querySelector(".saves");
saves.style.display = "none";
edits.style.display = "block";
delNull();
}
// 删除
function dels(e) {
var fuIndex = e.parentNode.parentNode;
console.log(fuIndex);
fuIndex.remove();
for (var i = 0; i < arr.length; i++) {
if (arr[i].id == fuIndex.children[4].innerHTML) {
arr.splice(i, 1);
}
}
fh();
}
// 序号
function fh() {
var trs = document.querySelectorAll("tr");
console.log(trs);
for (var i = 1; i < trs.length; i++) {
trs[i].children[0].innerHTML = i;
}
}
// 清空
function delNull() {
var inpus = document.querySelectorAll(".boss input");
for (var i = 0; i < inpus.length; i++) {
inpus[i].value = "";
inpus[i].removeAttribute("disabled");
}
}
//搜索
var sb = document.getElementsByClassName("sb")[0];
sb.onclick = function () {
var sou = document.getElementById("sousuo").value;
var trr2 = document.querySelectorAll("tr");
for (var i = 1; i < trr2.length; i++) {
if (sou == "" || sou == null || sou == undefined) {
trr2[i].style.display = "";
} else {
trr2[i].style.display = "none";
if (trr2[i].children[4].innerHTML == sou) {
trr2[i].style.display = "";
}
}
}
}
</script>
</html>
效果图: