10

我有一个 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 起个名字。

4

1 回答 1

6

您发布的代码工作正常!

这是一个演示:http: //jsfiddle.net/enXcn/1/

请注意,当您更改 slice 的值时,会显示更多或更少的元素,这就是您所描述的它应该做的事情。

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);
        });
于 2013-07-01T10:04:15.977 回答