javascript 在javascript中删除表格行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16258658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting table rows in javascript
提问by BooBailey
I'm having trouble with a function in javascript and can't figure out why. It's really quite straight forward. I'm trying to delete all the rows in a html table. so I wrote:
我在使用 javascript 中的函数时遇到问题,不知道为什么。这真的很简单。我正在尝试删除 html 表中的所有行。所以我写道:
function delete_gameboard(){
var table = document.getElementById("gameboard");
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
table.deleteRow(i);
}
}
Yet, it'll only delete half of them. Can anyone see what's causing this strange behavior?
然而,它只会删除其中的一半。谁能看出是什么导致了这种奇怪的行为?
回答by xdazz
Because when you delete the first row, the second row will become the first, it's dynamic.
因为当你删除第一行时,第二行会变成第一行,它是动态的。
You could do like:
你可以这样做:
while(table.rows.length > 0) {
table.deleteRow(0);
}
回答by Marc B
Consider this:
考虑一下:
index 0: row #1
index 1: row #2
index 2: row #3
index 3: row #4
index 4: row #5
you delete index #2 (row #3), so the DOM engine adjusts the index keys and you end up with:
您删除索引 #2(第 3 行),因此 DOM 引擎调整索引键,您最终得到:
index 0: row #1
index 1: row #2
index 2: row #4 <---hey! index #2 is still there
index 3: row #5
You're basically digging a hole in the spot where you're standing, so as you dig deeper, you naturally sink deeper and never see any progress... until you run out of rows to delete in the table.
你基本上是在你站立的地方挖一个洞,所以当你挖得更深时,你自然会沉得更深,永远看不到任何进展......直到你用完表中要删除的行。
回答by Niet the Dark Absol
If you delete the first row, the second row becomes the new first row.
如果删除第一行,第二行将成为新的第一行。
I prefer to do this:
我更喜欢这样做:
while(table.rows[0]) table.deleteRow(0);
回答by Arun
function delete_gameboard(){
var table = document.getElementById("gameboard");
var rowCount = table.rows.length;
for (var i=rowcount-1; i >=0; i--) {
table.deleteRow(i);
}
}
The index of the row changes when you delete it. Use a reverse loop. This will also be helpful if you are using any condition to delete rows. If you are deleting all rows,use the following
删除该行时,该行的索引会发生变化。使用反向循环。如果您使用任何条件删除行,这也将很有帮助。如果要删除所有行,请使用以下命令
while(table.rows.length) {
table.deleteRow(0);
}
回答by Musa
The table
rows is live, that is if you delete row 0 then the next row becomes row 0, just constantly delete row 0
该table
行是活的,那就是,如果你删除的行0,那么下一行变为一行0,只是不断地删除行0
function delete_gameboard(){
var table = document.getElementById("gameboard");
var rowCount = table.rows.length;
for (var i=0; i < rowCount; i++) {
table.deleteRow(0);
}
}
回答by IMRUP
function delRow()
{
var current = window.event.srcElement;
//here we will delete the line
while ( (current = current.parentElement) && current.tagName !="TR");
current.parentElement.removeChild(current);
}
回答by DISSONANCE
You could just go:
你可以去:
document.getElementById("gameboard").innerHTML = "";
回答by dinesh782
If you are using JQuery in your code then the easiest way to delete the rows is using the following code:
如果您在代码中使用 JQuery,那么删除行的最简单方法是使用以下代码:
$('#myTable tbody').html('');