使用 javascript 在表中动态添加一行,模糊表中的最后一行



我在引用"http://stackoverflow.com/questions/9045940/dynamically-adding-table-row"时创建了一个表,每当我进入表中的最后一行时,我都需要动态地将另一行添加到底部的表中,这是我尝试的代码:

<table id="myTable" style="table-layout:auto">
   <tr>
   <td><input type="text" onblur="addRow('myTable')" /></td>
    <td><input type="text"/></td>
    <td><input type="text"/></td>
  </tr>
</table>

JavaScript如下:

<script>
function addRow()
{
   //add a row to the rows collection and get a reference to the newly added row
   var newRow = document.all("myTable").insertRow();
   var oCell = newRow.insertCell();
   oCell.innerHTML = "<input type='text' onblur='addRow();'>";
   oCell = newRow.insertCell();
   oCell.innerHTML = "<input type='text' name='title'>";
   oCell = newRow.insertCell();
   oCell.innerHTML = "<input type='text' name='title'>";   
}
</script>

但是,这里在表格顶部添加了新行,我该怎么办?

更改var newRow = document.all("myTable").insertRow();

var newRow = document.all("myTable").insertRow(-1);

另外,在您的onblur事件中,为什么在不需要任何参数的情况下将参数传递给addRow函数?

最新更新