代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
table {
border-collapse: collapse;
text-align: center;
}
</style>
</head>
<body>
<input type="text" id="inp1">
<input type="text" id="inp2">
<button id="butt">添加</button>
<div id="tb">
<table border="1" width="300" id="table">
<thead>
<th>编号</th>
<th>品牌名称</th>
<th>创立时间</th>
<th>操作</th>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
</body>
</html>
<script>
var butt = document.getElementById("butt")
var inp1 = document.getElementById("inp1")
var inp2 = document.getElementById("inp2")
document.onkeydown = function (e) {
if (e.keyCode === 13) {
var a = inp1.value
var b = inp2.value
// if (a != /^[0-9]{5}$/) {
// alert("编号框错误,请输入正确的东西")
// inp1.value = ""
// return
// }
var c = {
id: a,
productName: b,
timer: new Date().toLocaleTimeString()
}
data.push(c)
fn()
inp1.value = ""
inp2.value = ""
}
}
var data = [{
id: 1,
productName: "品如的衣柜",
timer: new Date().toLocaleTimeString()
},
{
id: 2,
productName: "蔡徐坤的篮球",
timer: new Date().toLocaleTimeString()
},
{
id: 3,
productName: "关羽的马",
timer: new Date().toLocaleTimeString()
},
]
var tbody = document.getElementById("tbody")
function fn() {
tbody.innerHTML = ""
for (i = 0; i < data.length; i++) {
var tr = document.createElement("tr")
for (k in data[i]) {
var td = document.createElement("td")
td.innerText = data[i][k]
tr.appendChild(td)
}
var lastTd = document.createElement("td")
var btn = document.createElement("button")
btn.innerText = "删除"
lastTd.appendChild(btn)
tr.appendChild(lastTd)
tbody.appendChild(tr)
btn.index = i
}
btn.onclick = function () {
data.splice(this.index, 1)
fn(data)
}
}
fn(data)
</script>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。