admin管理员组

文章数量:1333188

I'd like to write <td> tags with JavaSctipt in my HTML code. I'm building a <table> in the main code and I'd like to continue it with a <script>, adding rows in the division.

<body>
  <table>
    <tr>
      <td>First</td>
    </tr>
    <div id="searchOutput"></div>
    <tr>
      <td>Last</td>
    </tr>
  </table>
  <script>
  document.getElementById("searchOutput").innerHTML = "<tr><td>Middle<td><tr>";
  </script>
</body>

The problem is that the <script> creates another table in a strange way.

Is there a way to add rows without writing all code (including <table> tags) in the <script>?

I'd like to write <td> tags with JavaSctipt in my HTML code. I'm building a <table> in the main code and I'd like to continue it with a <script>, adding rows in the division.

<body>
  <table>
    <tr>
      <td>First</td>
    </tr>
    <div id="searchOutput"></div>
    <tr>
      <td>Last</td>
    </tr>
  </table>
  <script>
  document.getElementById("searchOutput").innerHTML = "<tr><td>Middle<td><tr>";
  </script>
</body>

The problem is that the <script> creates another table in a strange way.

Is there a way to add rows without writing all code (including <table> tags) in the <script>?

Share Improve this question asked Jun 16, 2018 at 1:31 Marco Di FrancescoMarco Di Francesco 451 gold badge4 silver badges15 bronze badges 2
  • 4 table children should not be divs. Try selecting the table and appending a tr to it, or selecting an existing tr and putting tds/text into it – CertainPerformance Commented Jun 16, 2018 at 1:32
  • I already tried to insert the table data in various tag like <p> or <h1> and the problem was already there. I also tried to put a <td> in a <tr> but the result was always the same. – Marco Di Francesco Commented Jun 16, 2018 at 10:49
Add a ment  | 

3 Answers 3

Reset to default 3

For insert new row in the table, you can use Table insertRow() and insertCell() Methods. The insertRow() methods creates an empty <tr> element and adds it to a table. The insertCell() method inserts a cell into the current row.

See the code below:

function addRows() {
  var table = document.getElementById( 'myTable' ),
      row = table.insertRow(0),
      cell1 = row.insertCell(0),
      cell2 = row.insertCell(1);

  cell1.innerHTML = 'Cell 1';
  cell2.innerHTML = 'Cell 2';
}
table {
  border: 1px solid #999;
  border-collapse: collapse;
  width: 100%
}
td {
  border: 1px solid #999
}
<p>
  <button onclick="addRows()">Add a new row</button>
</p>
<table id="myTable"></table>

CertainPerformance is correct; divs should not be direct children of tables. You might find this question useful. You have the right idea, if only you could actually replace the HTML of the div as opposed to simply filling it in. So, you could set the ID of the Last tr to searchOutput. Then, something like

var newRow = document.createElement("tr");
var oldRow = document.getElementById("searchOutput");
newRow.innerHTML = "<tr><td>Middle</td></tr>";
document.getElementByTagName("table").insertBefore(row, oldRow);

Take a look at the example from w3cschools. https://www.w3schools./jsref/tryit.asp?filename=tryjsref_table_insertrow

本文标签: Add rows in HTML table with JavaScriptStack Overflow