【发布时间】:2018-05-26 00:31:48
【问题描述】:
我为在 HTML 页面中打印 JSON 编写了以下代码。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.getJSON("http://localhost:8080/json",function(result){
$.each(result, function(i, field){
$("tr").append(field + " ");
});
});
});
</script>
</head>
<body>
<table id="table">
<tr>
<th>market</th>
<th>buy</th>
<th>sell</th>
<th>currency</th>
<th>volume</th>
</tr>
</table>
<div></div>
</body>
</html>
我能够打印 JSON 响应值,但无法在表格中正确打印它们。
JSON 响应格式:
{
"market": 1309480,
"buy": 1309480,
"sell": 1280017,
"currency": "INR",
"volume": 2253.4518854
}
当前表格的外观:
【问题讨论】:
-
在
tr中不需要td吗?您还将它们附加到标题所在的同一位置。您应该使用thead和tbody。 -
@N.Ivanov 你能详细说明一下吗?
标签: jquery html json html-table getjson