gpt4 book ai didi

javascript - 如何在获取 json 条目时控制它

转载 作者:行者123 更新时间:2023-11-28 19:47:21 24 4
gpt4 key购买 nike

我的代码显示了所有 json 文件条目的图像、标题和摘要,这是我仅需要向第一个条目显示图像、标题和摘要并将向其他条目显示标题的代码。

请指教。

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
<script>

$(function() {

var entries = [];
var dmJSON = "http://localhost:8080/Newfolder/test.json";
$.getJSON( dmJSON, function(data) {
$.each(data.entries, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.image + "</td>" + "<td>" + f.title + "</td>" + "<td>" + f.summary + "</td>" + "</tr>"
$(tblRow).appendTo("#entrydata tbody");
});

});

});
</script>
</head>

<body>

<div class="wrapper">
<div class="profile">
<table id= "entrydata" border="1">
<thead>
<th>ID</th>
<th>UserName</th>
<th>Message</th>
<th>Location</th>
<th>Time</th>
</thead>
<tbody>

</tbody>
</table>

</div>
</div>

</body>

</html>

请修改我更新的代码:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
<script>

$(function() {

var items = [];
var dmJSON = "http://localhost:8080/Newfolder/realaljazeera.json";
$.getJSON( dmJSON, function(data) {
$.each(data.entries, function(i, f) {
if (i == 0) {
var tblRow = "<tr>" + "<td>" + "<img src="+f.Image3+"/>" + "</td>" + "<td>" + f.Title + "</td>" + "<td>" + f.Summary + "</td>" + "</tr>"
} else {
var tblRow = "<tr>" + "<td>" + f.Title + "</td>" + "</tr>"
}
$(tblRow).appendTo("#entrydata tbody");
});

});

});
</script>
</head>
<body>
<div class="wrapper">
<div class="profile">
<table id= "entrydata" border="2" style="width:680px;">
<tbody>
</tbody>
</table>
</div>
</div>
</body>
</html>

最佳答案

$.each(data.entries, function(i, f) {
if (i == 0) {
var tblRow = // what you want for first entry
} else {
var tblRow = // what you want for other entries
}
$(tblRow).appendTo("#entrydata tbody");
});

关于javascript - 如何在获取 json 条目时控制它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24055040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com