我有一个JSON文件,其中读取对象并在div中显示它们。但我只需要显示五个对象而不是全部。
以下是我正在使用的代码
$.each(recentActdata.slice(0,5), function(i, data) {
var ul_data = "<li><h3>"+ renderActionLink(data)+ "</h3></li>";
$("#recentActivities").append(ul_data);
});
但这片似乎不起作用。我的JSON格式是
[
{
"displayValue":"Updated Guidelines",
"link":"#",
"timestamp":"29/06/2013 01:32"
},
{
"displayValue":"Logging",
"link":"#",
"timestamp":"28/06/2013 16:19"
},
{
"displayValue":"Subscribe",
"link":"#",
"timestamp":"21/06/2013 14:30"
},
{
"displayValue":"Artifactory Vs Nexus",
"link":"#",
"timestamp":"21/06/2013 13:39"
},
{
"displayValue":"CTT - Java 7",
"link":"#",
"timestamp":"20/06/2013 13:30"
},
{
"displayValue":"Added Artifactory Server",
"link":"#",
"timestamp":"19/06/2013 23:39"
},
{
"displayValue":"Estimation Template",
"link":"#",
"timestamp":"19/06/2013 23:39"
},
{
"displayValue":"GZIP compression in Tomcat",
"link":"#",
"timestamp":"14/06/2013 23:39"
},
{
"displayValue":"HBase Basics",
"link":"#",
"timestamp":"12/06/2013 23:39"
}
]
请建议如何实现这一目标。我不能给我的JSON起名字。
答案 0 :(得分:5)
您发布的代码运作正常!
以下是演示:http://jsfiddle.net/enXcn/1/
请注意,当您更改切片的值时,会显示更多或更少的元素,这是您应该执行的操作。
HTML: <div id="recentActivities"></div>
JS:
var recentActdata = [
{
"displayValue":"Updated Guidelines",
"link":"#",
"timestamp":"29/06/2013 01:32"
},
{
"displayValue":"Logging",
"link":"#",
"timestamp":"28/06/2013 16:19"
},
{
"displayValue":"Subscribe",
"link":"#",
"timestamp":"21/06/2013 14:30"
},
{
"displayValue":"Artifactory Vs Nexus",
"link":"#",
"timestamp":"21/06/2013 13:39"
},
{
"displayValue":"CTT - Java 7",
"link":"#",
"timestamp":"20/06/2013 13:30"
},
{
"displayValue":"Added Artifactory Server",
"link":"#",
"timestamp":"19/06/2013 23:39"
},
{
"displayValue":"Estimation Template",
"link":"#",
"timestamp":"19/06/2013 23:39"
},
{
"displayValue":"GZIP compression in Tomcat",
"link":"#",
"timestamp":"14/06/2013 23:39"
},
{
"displayValue":"HBase Basics",
"link":"#",
"timestamp":"12/06/2013 23:39"
}
];
$.each(recentActdata.slice(0,5), function(i, data) {
var ul_data = "<li><h3>"+ data.displayValue+ "</h3></li>";
$("#recentActivities").append(ul_data);
});