admin管理员组

文章数量:1304155

Note: this is meant to be a munity wiki post

The following code using simple dom methods fails to add rows to the table. What's the issue?

<html>
<head>
<title>Javascript Test</title>
<script>
function addRow() {
    var mytable = document.getElementById('mytable');

    var row = document.createElement('tr');
    var cell = document.createElement('td');
    var text = document.createTextNode('This is a row');

    cell.appendChild(text);
    row.appendChild(cell);
    mytable.appendChild(row);
}
</script>
</head>
<body>
<form action="#">

<table id="mytable">
<tr>
    <td>This is a row</td>
</tr>
</table>

<input type="button" onclick="addRow()" value="Add A Row"/>
</form>
</body>
</html>

Note: this is meant to be a munity wiki post

The following code using simple dom methods fails to add rows to the table. What's the issue?

<html>
<head>
<title>Javascript Test</title>
<script>
function addRow() {
    var mytable = document.getElementById('mytable');

    var row = document.createElement('tr');
    var cell = document.createElement('td');
    var text = document.createTextNode('This is a row');

    cell.appendChild(text);
    row.appendChild(cell);
    mytable.appendChild(row);
}
</script>
</head>
<body>
<form action="#">

<table id="mytable">
<tr>
    <td>This is a row</td>
</tr>
</table>

<input type="button" onclick="addRow()" value="Add A Row"/>
</form>
</body>
</html>
Share Improve this question edited Dec 21, 2016 at 14:24 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 14, 2011 at 17:04 onteria_onteria_ 70.6k7 gold badges72 silver badges64 bronze badges 1
  • a more in-depth example for this: stackoverflow./a/19561902/2536357 – tuned Commented Oct 24, 2013 at 9:32
Add a ment  | 

2 Answers 2

Reset to default 9

The issue at hand here is that the <table> element's correct structure is not present. When dealing with tables, the basic structure is:

<table>
<thead>
<tr>
  <th>Heading for the table</th>
</tr>
</thead>
<tbody>
  <tr>
    <td>A row of data</td>
  </tr>
</tbody>
</table>

The logic is that when dealing with tables, you want to keep the labels of the columns, and the actual data separate. Because most browsers fill in the <tbody> as part of the process of fixing broken HTML, few people realize this. When the browser sees you adding a <tr>, it doesn't know if you're trying to add it to the <thead> or the <tbody>, so it fails.

The following shows the correct method for adding the rows:

<html>
<head>
<title>Javascript Test</title>
<script>
function addRow() {
    var mytbody = document.getElementById('mytbody');

    var row = document.createElement('tr');
    var cell = document.createElement('td');
    var text = document.createTextNode('This is a row');

    cell.appendChild(text);
    row.appendChild(cell);
    mytbody.appendChild(row);
}
</script>
</head>
<body>
<form action="#">

<table id="mytable">
<tbody id="mytbody">
<tr>
    <td>This is a row</td>
</tr>
</tbody>
</table>

<input type="button" onclick="addRow()" value="Add A Row"/>
</form>
</body>
</html>

Any additional row needs to be added then take the first child of tableID and then use appendChild() method:

var tbl=document.getElementById("tbl");
var row=document.createElement("tr");
var cel=document.createElement("td");
cel.innerHTML='sometext';
row.appendChild(cel);
tbl.children[0].appendChild(row);

本文标签: javascriptI39m trying to add some rows to an HTML tablebut it39s failingStack Overflow