首页 > 解决方案 > 向空的 tbody 元素添加行

问题描述

我有这样的表:

<table id="search-table" class="table">
  <thead>
    <tr>
      <th>Something</th>
      <th>Something2</th>
      <th>Something3</th>
      <th>Something4</th>
      <th>Something5</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

我有一个 API 调用,它返回我想作为行和列放在空 tbody 元素中的数据。

var body = document.getElementsbyTagName("tbody");
var x = 0;

for (i = 0; i < APIcall.length; i++) {
  var createRow = body.insertRow();
  for (j = 0; j < 7; j++) {
    var x = createRow.insertCell(j);
  }
}

如果我在 thead 元素上插入行,则会创建行,但当我尝试将其添加到 tbody 时不会。可能我只是误会了什么。有什么建议我应该怎么做?

标签: javascriptjquerydom

解决方案


var html ='';
for (i = 0; i < APIcall.length; i++) {`enter code hereenter code here
  html += '<tr><td>' + APIcall[i].somthing + '</td><td>'+ APIcall[i].somthing + '</td>' +
    '<td>' + APIcall[i].somthing + '</td>' + < td > '+APIcall[i].somthing+' </td>'+
  '<td>' + APIcall[i].somthing + '</td></tr>';

}
$('#search-table > tbody').append(html);

enter code here


推荐阅读