在javascript中删除表行 [英] Deleting table rows in javascript

查看:108
本文介绍了在javascript中删除表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用javascript中的函数时遇到问题,无法找出原因。这真的很直接。我正在尝试删除html表中的所有行。所以我写道:

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:

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?

推荐答案

因为当你删除第一行时,第二行将成为第一行,这是动态的。

Because when you delete the first row, the second row will become the first, it's dynamic.

你可以这样做:

while(table.rows.length > 0) {
  table.deleteRow(0);
}

这篇关于在javascript中删除表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆