Javascript 删除HTML表中除带有JS的标题之外的所有行
因此,在使用js删除HTML表中的所有数据时,如果不删除标题,就会遇到问题。这是我的HTML:Javascript 删除HTML表中除带有JS的标题之外的所有行,javascript,html,html-table,Javascript,Html,Html Table,因此,在使用js删除HTML表中的所有数据时,如果不删除标题,就会遇到问题。这是我的HTML: <html> <title>Z80 Opcodes</title> <body> <input type="text" id = "input" value = ""> <button onClick = "hex()">Hex</button> <button onClick = "bin()">Bin&
<html>
<title>Z80 Opcodes</title>
<body>
<input type="text" id = "input" value = "">
<button onClick = "hex()">Hex</button>
<button onClick = "bin()">Bin</button>
<button onClick = "assem()">Assembly</button>
<button onClick = "find()">Find</button>
<table id = "out" style="width:100%">
<thead>
<th align="left">Binary</th>
<th align="left">Hex</th>
<th align="left">Assembly</th>
<th align="left">Description</th>
</thead>
<tbody id="tb">
</tbody>
<script src = "js/script.js">
</script>
</body>
</html>
下面是script.js:
var id = document.getElementById("out");
var ab = "";
var row;
var table = ['0000000000nopno operation', '0000000101ld (bc), **loads ** into BC'];
var bin = ['00000000', '00000001'];
var hex = ['00', '01'];
var assembly = ['nop', 'ld (bc), **'];
var description = ['no operation', 'loads ** into BC'];
l = table.length;
function find() {
row = id.insertRow(0);
for (i=0;i <=l-1;i++) {
ab = table[i];
if (ab.includes(document.getElementById("input").value)) {
row = id.insertRow(-1);
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
cell3 = row.insertCell(2);
cell4 = row.insertCell(3);
cell1.innerHTML = bin[i];
cell2.innerHTML = hex[i];
cell3.innerHTML = assembly[i];
cell4.innerHTML = description[i];
}
}
}
我省略了很多数组项,因为它们包含了几乎完整的EZ80微处理器指令集。
这段代码基本上是从用户那里获取一个输入,如果数组表包含该输入,那么它从bin、hex、assembly和description中获取相应的值,并在表中为每一列赋值,然后为数据添加一行。但是,我似乎找不到一种方法来删除tbody中的所有行,然后在不删除ad的情况下将另一组值追加到表中。我在网上四处搜索,找到了几种解决方案,但没有一种有效。他们要么删除了thead,要么导致了某种错误,如果你问的话,我会举一些例子。我正在使用Google Chrome 63.0.3239.132版web浏览器运行我的代码。
正如您可能看到的,我对js和HTML非常陌生
你知道我做错了什么吗
~Jachdich从语义的角度来看,如果您将行添加到元素而不是元素中,则删除行会更容易。通过这种方式,您可以将主体中的行作为目标并将其删除,同时保留您的同级节点:
var body = document.querySelector('tbody');
while (body.firstChild) {
// This will remove all children within tbody which in your case are <tr> elements
body.removeChild(body.firstChild);
}
body.removechildrows与体内
从语义的角度来看,如果您将行添加到元素而不是元素中,那么删除行将更加容易。通过这种方式,您可以将主体中的行作为目标并将其删除,同时保留您的同级节点:
var body = document.querySelector('tbody');
while (body.firstChild) {
// This will remove all children within tbody which in your case are <tr> elements
body.removeChild(body.firstChild);
}
body.removechildrows与体内
假设您希望保持tbody元素的完整性,而不是一次删除它及其所有子元素:
// get a reference to the tbody element
var tb = document.querySelector('#out tbody');
// while tb has children, remove the first one
while (tb.childNodes.length) {
tb.removeChild(tb.childNodes[0]);
}
// tb is now empty
假设您希望保持tbody元素的完整性,而不是一次删除它及其所有子元素:
// get a reference to the tbody element
var tb = document.querySelector('#out tbody');
// while tb has children, remove the first one
while (tb.childNodes.length) {
tb.removeChild(tb.childNodes[0]);
}
// tb is now empty
首先,你必须关闭你的标签 这就是你清理桌子的方法 $“删除”。单击时,=>{ $'tb'。为空 } 二进制的 十六进制 装配 描述 01010101 1231:sdf:sdf42 213123 描述 清楚的
首先,你必须关闭你的标签 这就是你清理桌子的方法 $“删除”。单击时,=>{ $'tb'。为空 } 二进制的 十六进制 装配 描述 01010101 1231:sdf:sdf42 213123 描述 清楚的
document.querySelector'out tbody'.removedocument.querySelector'out tbody'.removequerySelector返回与选择器匹配的第一个元素,这样只会删除第一行querySelector返回与选择器匹配的第一个元素,这样只会删除第一行我有一点格式错误,标记在我的代码中,我一定是意外删除了问题中的标记我希望此代码段可以帮助您:我有一点格式错误,标记在我的代码中,我一定是意外删除了问题中的标记我希望此代码段可以帮助您: