Jér*_*nge 112 html javascript row html-table insert
我有一个带有页眉和页脚的HTML表格:
<table id="myTable">
<thead>
<tr>
<th>My Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>aaaaa</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>My footer</td>
</tr>
<tfoot>
</table>
Run Code Online (Sandbox Code Playgroud)
我正在尝试tbody使用以下内容添加一行:
myTable.insertRow(myTable.rows.length - 1);
Run Code Online (Sandbox Code Playgroud)
但该行添加在该tfoot部分中.
如何插入tbody?
Jon*_*uin 186
如果要在其中添加行tbody,请获取对它的引用并将其添加到那里.
var tableRef = document.getElementById('myTable').getElementsByTagName('tbody')[0];
// Insert a row in the table at the last row
var newRow = tableRef.insertRow();
// 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 row');
newCell.appendChild(newText);
Run Code Online (Sandbox Code Playgroud)
一个工作演示在这里.您也可以查看insertRow 此处的文档.
Sas*_*ran 22
您可以使用Jquery尝试使用代码段.
$(table).find('tbody').append("<tr><td>aaaa</td></tr>");
Run Code Online (Sandbox Code Playgroud)
基本方法:
这应该添加html格式的内容并显示新添加的行.
var myHtmlContent = "<h3>hello</h3>"
var tableRef = document.getElementById('myTable').getElementsByTagName('tbody')[0];
var newRow = tableRef.insertRow(tableRef.rows.length);
newRow.innerHTML = myHtmlContent;
Run Code Online (Sandbox Code Playgroud)
你很接近,只需将行添加到tbody而不是table:
myTbody.insertRow();
Run Code Online (Sandbox Code Playgroud)
在使用之前获取对tBody(myTbody)的引用.请注意,您不需要传递表中的最后一个位置,在省略参数时它会自动定位在最后.
小智 8
let myTable = document.getElementById('myTable').getElementsByTagName('tbody')[0];
let row = myTable.insertRow();
let cell1 = row.insertCell(0);
let cell2 = row.insertCell(1);
let cell3 = row.insertCell(2);
cell1.innerHTML = 1;
cell2.innerHTML = 'JAHID';
cell3.innerHTML = 23;
row = myTable.insertRow();
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
cell3 = row.insertCell(2);
cell1.innerHTML = 2;
cell2.innerHTML = 'HOSSAIIN';
cell3.innerHTML = 50;Run Code Online (Sandbox Code Playgroud)
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #000;
padding: 10px;
}Run Code Online (Sandbox Code Playgroud)
<table id="myTable">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>AGE</th>
</tr>
</thead>
<tbody></tbody>
</table>Run Code Online (Sandbox Code Playgroud)
我认为这个脚本正是你需要的
var t = document.getElementById('myTable');
var r =document.createElement('TR');
t.tBodies[0].appendChild(r)
Run Code Online (Sandbox Code Playgroud)
小智 5
添加行:
<html>
<script>
function addRow() {
var table = document.getElementById('myTable');
//var row = document.getElementById("myTable");
var x = table.insertRow(0);
var e = table.rows.length-1;
var l = table.rows[e].cells.length;
//x.innerHTML = " ";
for (var c=0, m=l; c < m; c++) {
table.rows[0].insertCell(c);
table.rows[0].cells[c].innerHTML = " ";
}
}
function addColumn() {
var table = document.getElementById('myTable');
for (var r = 0, n = table.rows.length; r < n; r++) {
table.rows[r].insertCell(0);
table.rows[r].cells[0].innerHTML = " ";
}
}
function deleteRow() {
document.getElementById("myTable").deleteRow(0);
}
function deleteColumn() {
// var row = document.getElementById("myRow");
var table = document.getElementById('myTable');
for (var r = 0, n = table.rows.length; r < n; r++) {
table.rows[r].deleteCell(0); // var table handle
}
}
</script>
<body>
<input type="button" value="row +" onClick="addRow()" border=0 style='cursor:hand'>
<input type="button" value="row -" onClick='deleteRow()' border=0 style='cursor:hand'>
<input type="button" value="column +" onClick="addColumn()" border=0 style='cursor:hand'>
<input type="button" value="column -" onClick='deleteColumn()' border=0 style='cursor:hand'>
<table id='myTable' border=1 cellpadding=0 cellspacing=0>
<tr id='myRow'>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
还有细胞。
小智 5
您还可以使用querySelector选择正文,然后在其末尾插入新行。
用于append将 Node 或 DOMString 对象插入到新单元格中,然后将其插入到新行中。
var myTbody = document.querySelector("#myTable>tbody");
var newRow = myTbody.insertRow();
newRow.insertCell().append("New data");Run Code Online (Sandbox Code Playgroud)
<table id="myTable">
<thead>
<tr>
<th>My Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>My footer</td>
</tr>
</tfoot>
</table>Run Code Online (Sandbox Code Playgroud)