jQuery分页插件

希望这是一个容易补救的东西。 我在理解jQuery Pagination插件时遇到了一些问题。

基本上,我要做的就是加载一个PHP文件,然后对结果进行分页。 我试图摆脱他们的榜样,但我并没有屈服于我正在寻找的结果。

这是JavaScript:

function pageselectCallback(page_index, jq){ var new_content = $('#hiddenresult div.result:eq('+page_index+')').clone(); $('#Searchresult').empty().append(new_content); return false; } function initPagination() { var num_entries = $('#hiddenresult div.result').length; // Create pagination element $("#Pagination").pagination(num_entries, { num_edge_entries: 2, num_display_entries: 8, callback: pageselectCallback, items_per_page:3 }); } $(document).ready(function(){ $('#hiddenresult').load('load.php', null, initPagination); }); 

这是我的HTML(加载PHP之后):

   
Result #1
Result #2
Result #3
Result #4
Result #5
Result #6
Result #7

基本上,我试图每页显示“3”项,但这不起作用。 我假设在某个地方,我将需要在我的JS中创建一个for循环,但我很困惑如何这样做。 文档可以在这里找到 。

你甚至不需要使用for循环,只需使用jQuery的slice()方法和一些数学运算。

我在JS Bin上主持了一个工作演示: http : //jsbin.com/upuwe (可编辑自http://jsbin.com/upuwe/edit )

这是修改后的代码:

 var pagination_options = { num_edge_entries: 2, num_display_entries: 8, callback: pageselectCallback, items_per_page:3 } function pageselectCallback(page_index, jq){ var items_per_page = pagination_options.items_per_page; var offset = page_index * items_per_page; var new_content = $('#hiddenresult div.result').slice(offset, offset + items_per_page).clone(); $('#Searchresult').empty().append(new_content); return false; } function initPagination() { var num_entries = $('#hiddenresult div.result').length; // Create pagination element $("#Pagination").pagination(num_entries, pagination_options); }