簡體   English   中英

使用javascript動態地在表格的最后一行中添加一行

[英]Dynamically adding a row in the table onblur the last row in the table using 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>

但是,這里在表格的頂部添加了一個新行,我該怎么辦?

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

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

另外,在你的onblur事件中,為什么在不帶任何參數的情況下將參數傳遞給addRow函數呢?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM