如何使用javascript将行添加到表的开头

如何使用javascript将行添加到表的开头,javascript,Javascript,我克隆一行并将其附加到表的末尾,使用以下函数add_record()。 我的问题是如何将其附加到表的开头。 我试着用 $("#main_table_id tbody").prepend(clone) 及 但它不起作用 function add_record(){ var row = document.getElementById("template_no_1_id"); var table = document.getElementById("m

我克隆一行并将其附加到表的末尾,使用以下函数add_record()。 我的问题是如何将其附加到表的开头。 我试着用

$("#main_table_id tbody").prepend(clone) 

但它不起作用

function add_record(){      
        var row = document.getElementById("template_no_1_id"); 
        var table = document.getElementById("main_table_id"); 
        var clone = row.cloneNode(true); 
        clone.id = "newID"; 
        table.appendChild(clone); 
    }

使用本机函数insertRow

<table id="TableA">
<tr>
<td>Old top row</td>
</tr>
</table>
<script type="text/javascript">

function addRow(tableID) {
  // Get a reference to the table
  var tableRef = document.getElementById(tableID);

  // Insert a row in the table at row index 0
  var newRow   = tableRef.insertRow(0);

  // Insert a cell in the row at index 0
  var newCell  = newRow.insertCell(0);

  // Append a text node to the cell
  var newText  = document.createTextNode('New top row');
  newCell.appendChild(newText);
}

// Call addRow() with the ID of a table
addRow('TableA');

</script>

老顶排
函数addRow(tableID){
//获取对该表的引用
var tableRef=document.getElementById(tableID);
//在表中的行索引0处插入一行
var newRow=tableRef.insertRow(0);
//在索引0处的行中插入单元格
var newCell=newRow.insertCell(0);
//将文本节点附加到单元格
var newText=document.createTextNode(“新的顶行”);
appendChild(newText);
}
//使用表的ID调用addRow()
addRow(“表A”);

信用证:

可能重复的答案的可能重复项将重新显示在表的所有内部HTML中。我想要一个答案,只追加第一行,因为表是大的。
<table id="TableA">
<tr>
<td>Old top row</td>
</tr>
</table>
<script type="text/javascript">

function addRow(tableID) {
  // Get a reference to the table
  var tableRef = document.getElementById(tableID);

  // Insert a row in the table at row index 0
  var newRow   = tableRef.insertRow(0);

  // Insert a cell in the row at index 0
  var newCell  = newRow.insertCell(0);

  // Append a text node to the cell
  var newText  = document.createTextNode('New top row');
  newCell.appendChild(newText);
}

// Call addRow() with the ID of a table
addRow('TableA');

</script>