我在引用"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
函数?