简单的jQuery分页

jac*_*per 4 html javascript css jquery pagination

我有一个大约有50个div的页面.我想将这些div组织成六个一组,以便客户端不会"信息过载".我已经为我的问题创建了一个简单的示例/简化测试用例.正如你所看到的,有很多div,我想要它,这样当页面加载时,只有前六个是可见的,但是当你点击第2页或下一页时,接下来的六个就会显示出来.您所使用的页码类也应设置为具有class="current".

到目前为止,我已经尝试过使用jQuery,但是我已经陷入困境了!任何帮助将非常感激!

geo*_*org 22

请求页面时,隐藏所有内容div,然后遍历它们并显示应出现在"页面"上的内容:

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

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


小智 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);

});?
Run Code Online (Sandbox Code Playgroud)

  • 任何时候你看到像'$('.content')的代码.eq((page-1)*6).show().next().show().next().show().next(). show().next().show().next().show();`它是使用循环的候选者.只是在说' (2认同)