我想創建一個Ajax函數,它將根據創建日期創建文章導航,因此用戶可以使用以前(較舊)和下一個(較新)鏈接。使用ajax請求獲取上一個和下一個對象
<div class="content clear">
<div class="article">
Article contents...
</div>
<a href="#Prev" id="prev" class="leftarrow" title="Previous">EELMINE<span class="arrow_left"></span></a>
<a href="#" id="bactolist" onClick="history.go(-1); return false;">Tagasi nimekirja juurde<span class="arrow_middle"></span></a>
<a href="#Next" id="next" class="rightarrow" title="Next">JÄRGMINE<span class="arrow_right"></span></a>
</div> <!-- End content -->
<script type="text/javascript">
$.ajax({
url: '/admin/api/site/articles.json?per_page=100',
dataType: 'json',
success: function(articles) {
$(articles).each(function(index, article) {
console.log(article);
$('div.article').fadeOut(0);
$('div.article:first').fadeIn(500);
$('a.leftarrow, a.rightarrow').click(function (ev) {
//prevent browser jumping to top
ev.preventDefault();
//get current visible item
var $visibleItem = $('div.article:visible');
//get total item count
var total = $('div.article').length;
//get index of current visible item
var index = $visibleItem.prevAll().length;
//if we click next increment current index, else decrease index
$(this).attr('href') === '#Next' ? index++ : index--;
//if we are now past the beginning or end show the last or first item
if (index === -1){
index = total-1;
}
if (index === total){
index = 0
}
//hide current show item
$visibleItem.hide();
//fade in the relevant item
$('div.article:eq(' + index + ')').fadeIn(500);
});
});
}
});
我有建設負責根據他們的日期值獲得物品功能的困難。
使用console.log(article)
,我得到以下值:
body: "..."
comments_count: 0
comments_url: "..."
created_at: "date and time ..."
excerpt: "..."
title: "..."
url: "..."
所以應該可以使用created_at
變量導航,但我沒有在一分鐘知道怎麼辦。 任何想法?
使用的CMS:Edicy 注意: CMS不支持PHP。
編輯:文章列表sample在由CMS開發者文檔提供的「博客」頁面上。
什麼是 'EV' 你是在調用preventDefault()方法? – Alex
我用它來防止帶錨標籤'href =「#」'的默認事件行爲。 – Laniakea
@Alex由於代碼格式不太明顯,但上面的行設置了一個單擊事件處理程序。 –