我的代码需要帮助。我想要一个上一个和一个下一个按钮,这些按钮将调用函数 viewBlogItem(direction,cat,blogid);
在该函数中,我将读出 json 文件,并按“类别”进行分类。
每个 blogItem 都有一个articleid 和一个类别,如果单击“下一个”按钮,我想要下一个 blogItem.articleid 并显示该条目(我对该条目使用“附加”)。如果方向==“下一个”,那么它会查看类别中是否有下一个项目,如果没有则隐藏$('.next')。上一个按钮也是如此 $('.previous')
博客项目.json
{
"blogItem":[
{
"id": 1,
"title": "animals blog 1",
"category":"animals",
"text":"text",
"articleid":1
},
{
"id": 2,
"title": "lifestyle blog 1",
"category":"lifestyle",
"text":"text",
"articleid": 1
},
{
"id": 3,
"title": "animals blog 2",
"category":"animals",
"text":"text",
"articleid": 2
},
{
"id": 5,
"title": "animals blog 4",
"category":"dieren",
"text":"text",
"articleid":4
},
{
"id": 4,
"title": "animals blog 5",
"category":"animals",
"text":"text",
"articleid":3
}
]
}
jquery
function viewBlogItem(direction,cat,blogid) {
var id = "";
if(direction == "next"){
// code for showing next blogitem
//if no next then
$('').hide();
}
else{
// if no previous then hide
//code for showing previous blogitem
}
loadBlog(id);
}
function loadBlog(blogid){
$.getJSON('blogitems.json', function (data) {
$.each(data.blogItem, function (i, item) {
if (item.id == blogid) {
$('.view').append('all sorts of stuff');
return;
}
});
});
}
我还想对我的 json 结构有一些建议。