I want to create a new row in the below tbody
but after the first row ( "aaaaa"
).
<html>
<body onload="generate()">
<table id="myTable">
<thead>
<tr>
<th>My Header</th>
</tr>
</thead>
<tbody id="myList">
<tr>
<td>aaaaa</td>
</tr>
</tbody>
</table>
</body>
<script>
function generate(){
var node = document.createElement("tr");
node.setAttribute("id","one");
var textnodeTD = document.createElement("td");
var values = document.createTextNode("AAAAA");
document.getElementById("myList").lastChild(node);
document.getElementById("one").appendChild(textnodeTD);
textnodeTD.appendChild(values);
}
</script>
</html>
Change the lastChild
to appendChild
function generate() { var node = document.createElement("tr"); node.setAttribute("id", "one"); var textnodeTD = document.createElement("td"); var values = document.createTextNode("AAAAA"); document.getElementById("myList").appendChild(node); document.getElementById("one").appendChild(textnodeTD); textnodeTD.appendChild(values); }
<body onload="generate()"> <table id="myTable"> <thead> <tr> <th>My Header</th> </tr> </thead> <tbody id="myList"> <tr> <td>aaaaa</td> </tr> </tbody> </table> </body>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.