我有这个功能来显示博客条目:
function getPrev(item, articles) {
for (var i = 0, len = articles.length; i < len; i++) {
if (articles[i].id === item) {
return (i - 1 >= 0) ? articles[i - 1] : null;
}
}
return null;
}
这一个:
function getNext(item, articles) {
for (var i = 0, len = articles.length; i < len; i++) {
if (articles[i].id === item) {
return (typeof (articles[i + 1]) !== 'undefined') ? articles[i + 1] : null;
}
}
return null;
}
最后是ajax电话:
function getArticle(which) {
$.ajax({
type: "GET",
url: "../Content/test.txt",
dataType: "json",
success: function (data) {
var entry;
var id = parseInt(document.getElementById('blogcont').getAttribute('data-id')) || 0;
if (which === 'next') {
entry = getNext(id, data);
} else if (which === 'prev') {
entry = getPrev(id, data);
} else {
entry = data[0];
}
if (entry) {
document.querySelector('#blogcont .headline').innerHTML = entry.Title;
document.querySelector('#blogcont .content').innerHTML = entry.Content;
document.getElementById('blogcont').setAttribute('data-id', entry.id);
}
},
error: function (data) {
console.error('Error in AJAX call');
console.error(data);
}
});
}
我希望在显示当前时显示左侧面板中的前一个(类似)。有什么想法吗?我的HTML:
<button class="nvgt" id="prev">Previous</button>
<button class="nvgt" id="next">Next</button>
</div>
<div id="articles">
<div id="similar" class="box"></div>
<div id="popular" class="box"></div>
<div id="thisblog" class="box"></div>
</div>
由于