【发布时间】:2019-01-14 01:18:15
【问题描述】:
我的 html 文件中有一个 json 代码传入,我正在尝试通过 javascript 将表格行添加到基本 .html 文件中的 html 表格的顶部
这是我的 javascript 代码:
var data = json.data;
for (var i = 0, len = data.length; i < len; i++) {
var x = data[i];
var newItem = document.createElement("tr");
var textnode = document.innerHTML = '<th scope="row"> ' + x.granularity + '</th> <td> ' + x.instrument + '</td> <td>complete: ' + x.complete + ', type: ' + x.type + ', alerttimedate: ' + x.alerttimedate + ', firsttopbot: ' + x.firsttopbot + ', alertprice: ' + x.alertprice + ', firsttbprice: ' + x.firsttbprice + ', timestamp: ' + x.timestamp + ', proceesed: ' + x.proceesed + '</td>';
newItem.appendChild(textnode);
var list = document.getElementById("myList");
list.insertBefore(newItem, list.childNodes[0]);
}
这是 html 文件中的表格:
<table class="table table-striped" id="myList">
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
</table>
var textnode = document.innerHTML 内容没有向我的 html 文件添加任何元素??
任何想法我在这里做错了什么?
json 数据来自这种格式的 php 文件
foreach ($result as $row) {
$output[] = [
'instrument' => $row['instrument'],
'granularity' => $row['granularity'],
'complete' => $row['complete'],
'type' => $row['type'],
'alerttimedate' => $row['alerttimedate'],
'firsttopbot' => $row['firsttopbot'],
'alertprice' => $row['alertprice'],
'firsttbprice' => $row['firsttbprice'],
'timestamp' => $row['timestamp'],
'proceesed' => $row['proceesed']];
$lastId = $row['id'];
}
echo json_encode([
'status' => true,
'data' => $output
]);
【问题讨论】:
-
您能分享一些示例 json 数据,以便我们轻松地重新创建它吗? (只需几个项目就足够了,不需要全部数据)
-
刚刚添加了json数据格式
-
应该将
innerHTML分配给newItem
标签: javascript html arrays object