分页--pagination.js

var pagination = function (thispage, totalpage, ulele, firstlast) {
    ulele.html('');
    var prevCss, nextCss, firstCss, lastCss;

    firstCss = $('<li></li>', {
        "class": thispage == 1 ? 'prev disabled' : 'prev'
    }).append($(thispage == 1 ? '<span></span>' : '<a></a>', thispage == 1 ? {} : {"href": location.href.addParam('page', 1)}).html("首页"));
    lastCss = $('<li></li>', {
        "class": thispage == totalpage ? 'next disabled' : 'next'
    }).append($(thispage == totalpage ? '<span></span>' : '<a></a>', thispage == totalpage ? {} : {"href": location.href.addParam('page', totalpage)}).html("末页"));
    prevCss = $('<li></li>', {
        "class": thispage == 1 ? 'prev disabled' : 'prev'
    }).append($(thispage == 1 ? '<span></span>' : '<a></a>', thispage == 1 ? {} : {"href": location.href.addParam('page', totalpage - 1)}).html("&lt;上一页"));
    nextCss = $('<li></li>', {
        "class": thispage == totalpage ? 'next disabled' : 'next'
    }).append($(thispage == totalpage ? '<span></span>' : '<a></a>', thispage == totalpage ? {} : {"href": location.href.addParam('page', thispage + 1)}).html("下一页&gt;"));

    if(firstlast) firstCss.appendTo(ulele);
    prevCss.appendTo(ulele);

    if (thispage > 3) {
        $('<li></li>').append($('<a></a>', {"href": location.href.addParam('page', 1)}).html(1)).appendTo(ulele);
        $('<li><a><span>...</span></a></li>').appendTo(ulele);
    }
    for (var i = 1; i <= totalpage; i++) {
        if (i < thispage - 3 || i > thispage + 3) {
            continue;
        }
        var isActive = thispage == i ? 'active' : '';
        $('<li></li>', {
            "class": isActive
        }).append($('<a></a>', {"href": location.href.addParam('page', i)}).html(i)).appendTo(ulele);
    }
    if (totalpage - thispage > 3) {
        $('<li><a><span>...</span></a></li>').appendTo(ulele);
        $('<li></li>').append($('<a></a>', {"href": location.href.addParam('page', totalpage)}).html(totalpage)).appendTo(ulele);
    }

    nextCss.appendTo(ulele);
    if(firstlast) lastCss.appendTo(ulele);
}
原文地址:https://www.cnblogs.com/zouke1220/p/8176134.html