JavaScript - 在tbody标记后添加添加行的行,需要在tbody

时间:2015-06-03 11:09:34

标签: javascript html css twitter-bootstrap

在JavaScript中添加行时出现问题。一切正常,但在tbody标签后添加了行。我需要添加内部。

段:

function deleteRow(row) {
  var i = row.parentNode.parentNode.rowIndex;
  document.getElementById('add-row').deleteRow(i);
}

function insRow() {
  var x = document.getElementById('add-row');
  var new_row = x.rows[1].cloneNode(true);
  var len = x.rows.length;
  new_row.cells[0].innerHTML = len;

  var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
  inp1.id += len;
  inp1.value = '';
  x.appendChild(new_row);
}
<table class="table  table-hover" id="add-row">
  <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th style="text-align:right;">Delete</th>
    </tr>
  </thead>
  <tbody id="add-row1">
    <tr>
      <td>1</td>
      <td>
        <input type="text" class="form-control input-sm" placeholder="Enter Group Name" id="group" />
      </td>
      <td class="pull-right">
        <button class="btn btn-sm btn-danger" id="delPOIbutton" onclick="deleteRow(this)"><i class="fa fa-close"></i> Delete</button>
      </td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td colspan="3">
        <button class="btn btn-sm btn-success pull-right"><i class="fa fa-save"></i> Save</button>
        <button class="btn btn-sm btn-primary" id="addmorePOIbutton" onclick="insRow()"> <i class="fa fa-plus"></i> Add</button>
      </td>
    </tr>
  </tfoot>
</table>

1 个答案:

答案 0 :(得分:2)

您可以使用tbody的ID代替表id来追加孩子。使用table的id生成行并使用tbody来appendChild

Live Demo

function insRow() {
    var x = document.getElementById('add-row');
    var x1 = document.getElementById('add-row1');
    var new_row = x.rows[1].cloneNode(true);
    var len = x.rows.length;
    new_row.cells[0].innerHTML = len;

    var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
    inp1.id += len;
    inp1.value = '';
    x1.appendChild(new_row);
}