2012-04-05 32 views
3

我有一個約50 div的頁面。我希望將這些div分成六組,以便客戶不會得到「信息超載」。我創建了一個簡單的example/reduced test case我的問題。正如你所看到的,有很多divs,我希望它能夠在頁面加載的時候,只有前六個可見,但是當你點擊第2頁或者第2頁時,接下來的六個就會顯示出來。您所在頁碼的類別也應設置爲class="current"簡單的jQuery分頁

到目前爲止,我已經嘗試使用jQuery,但我一直在卡住!任何幫助將非常感激!

+0

你有沒有想過一個標籤式的看法,而不是一個分頁。我只是覺得在一個網格的情況下分頁更有意義,但是爲了劃分完整的內容我認爲tabview更好 – 2012-04-05 19:35:37

+0

是的你是對的,在你的情況下分頁更有意義。 – 2012-04-05 19:39:06

+0

這裏有大量的jQuery插件。例如:http://www.jquery4u.com/plugins/10-jquery-pagination-plugins/ – webdreamer 2012-04-05 19:48:56

回答

19

當請求一個頁面,隱藏所有內容的div,然後遍歷它們並顯示這些應該出現在「頁」上:

showPage = function(page) { 
    $(".content").hide(); 
    $(".content").each(function(n) { 
     if (n >= pageSize * (page - 1) && n < pageSize * page) 
      $(this).show(); 
    });   
} 

http://jsfiddle.net/jfm9y/184/

+0

完全同意...但你的下一個和prev按鈕不工作在你的jsfiddle .. – blackhawk 2013-05-01 20:59:11

+0

有疑問爲什麼你想要使用parseInt雖然你可以顯示文本通過使用this.text() – Benjamin 2014-07-31 09:17:20

+0

幫助了我很多!謝謝! :) – 2014-09-11 09:46:05

5

這部分代碼是不漂亮但我認爲它的工作

var currentpage = 1; 
var pagecount = 0; 

function showpage(page) { 
    $('.content').hide(); 
    $('.content').eq((page-1)*6).show().next().show().next().show().next().show().next().show().next().show(); 
    $('#pagin').find('a').removeClass('current').eq(page).addClass('current'); 

} 

$("#pagin").on("click", "a", function(event){ 
    event.preventDefault(); 
    if($(this).html() == "next") { 
     currentpage++; 
    } 
    else if($(this).html() == "prev") { 
     currentpage--; 
    } else { 
      currentpage = $(this).html(); 
    } 
    if(currentpage < 1) {currentpage = 1;} 
    if(currentpage > pagecount) {currentpage = pagecount;} 
    showpage(currentpage); 
});                 

$(document).ready(function() { 
    pagecount = Math.floor(($('.content').size())/6); 
    if (($('.content').size()) % 6 > 0) { 
     pagecount++; 
    } 

    $('#pagin').html('<li><a>prev</a></li>'); 
    for (var i = 1; i <= pagecount; i++) { 
     $('#pagin').append('<li><a class="current">' + i + '</a></li>'); 
    } 
    $('#pagin').append('<li><a>next</a></li>'); 
    showpage(1); 

});​ 
+0

任何時候你看到'$('。content')。eq((page-1)* 6).show()。next()。show()。next()。show()。 ().show()。next()。show()。next()。show();'這是一個使用循環的候選人。只是在說' – 2014-10-30 21:54:39