1 回答
TA贡献1772条经验 获得超8个赞
您想要做的是创建一个table元素并将其追加到 ,div然后生成一个字符串html,然后在循环结束后您想要将该字符串追加到 。div在文档加载后也可以使用以下命令运行 Javascript$(document).ready(function(){});
另外,有些情况下dataitem._embedded.show.network为 null,因此您想跳过那些无法访问 null 属性的数据附加(我还附加了一个string指示其用途的文件td,如果需要,您可以将其删除)
$(document).ready(function() {
var foo = null;
var data = "";
fetch("http://api.tvmaze.com/schedule/full")
.then(resp => resp.json())
.then(json_data => {
var table = document.createElement("table");
table.id = "mytable";
$("#div1").append(table);
for (let dataitem of json_data) {
data += "<tr>";
data += "<td>" + "id=" + dataitem.id + "</td>";
data += "<td>" + "name=" + dataitem.name + "</td>";
data += "<td>" + "image=" + dataitem.image + "</td></tr>";
data += "<tr><td>" + "summary=" + dataitem.summary + "</td></tr>";
data += "<tr><td>" + "airdate=" + dataitem.airdate + "</td></tr>";
data += "<tr><td>" + "airtime=" + dataitem.airtime + "</td></tr>";
data += "<tr><td>" + "airstamp=" + dataitem.airstamp + "</td></tr>";
data += "<tr><td>" + "runtime=" + dataitem.runtime + "</td></tr>";
data += "<tr><td>" + "season=" + dataitem.season + "</td></tr>";
data +=
"<tr><td>" +
"language=" +
dataitem._embedded.show.language +
"</td></tr>";
data +=
"<tr><td>" +
"genres=" +
dataitem._embedded.show.genres +
"</td></tr>";
if (dataitem._embedded.show.network !== null) {
data +=
"<tr><td>" +
"Country Name=" +
dataitem._embedded.show.network.country.name +
"</td></tr>";
data +=
"<tr><td>" +
"Code=" +
dataitem._embedded.show.network.country.code +
"</td></tr>";
data +=
"<tr><td>" +
"TimeZone=" +
dataitem._embedded.show.network.country.timezone +
"</td></tr>";
}
data +=
"<tr><td>" + dataitem._embedded.show.rating.average + "</td></tr>";
}
data += "</table>";
$("#mytable").append(data);
});
});
#mytable,
td {
border: 1px solid blue;
}
<!DOCTYPE html>
<html>
<head>
<!-- jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Bootstrap 4 JS -->
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
crossorigin="anonymous"
></script>
<!-- CSS File Here -->
<link rel="stylesheet" href="./index.css" />
</head>
<body>
<div id="div1"></div>
<script src="./index.js"></script>
</body>
</html>
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报