影子互联

专注深圳网站建设

一段不错的js分页代码

时间:2016-08-10 21:06:32,点击:0

function getPagingHtml(recordCount, pageCount, pageNum, pageSize, showNumCount, url) {
    var str = "";
    if (pageCount > 1) {
        //***前面部分
        if (pageNum > 1) {
            str += "<a href='" + url.replace('$num$', (pageNum - 1)) + "'>上一页</a>";
        }
 
        if (pageNum > showNumCount) {
            str += "<a href='" + url.replace('$num$', '1') + "'>1...</a>";
        }
        //*** 中间部分(根据算法.循环输出数字页码)
        var statrNum = Math.floor(pageNum / showNumCount) * showNumCount;
        if ((pageNum % showNumCount) == 0) {
            statrNum -= showNumCount;
        }
        var num, i;
        for (i = 1; i <= showNumCount; i++) {
            num = statrNum + i;
            if (num > pageCount) {
                break;
            }
            if (num == pageNum) {
                str += "<a href='" + url.replace("$num$", num) + "' class='m-paging-focus'>" + num + "</a>";
            }
            else {
                str += "<a href='" + url.replace("$num$", num) + "'>" + num + "</a>";
            }
        }
        //***结束部分
        if (pageCount > showNumCount) {
            var lastNumCount = pageCount % showNumCount;
            if (lastNumCount == 0) {
                lastNumCount = showNumCount;
            }
            if ((pageCount - pageNum) >= lastNumCount) {
                str += "<a href='" + url.replace("$num$", pageCount) + "'>..." + pageCount + "</a>";
            }
        }
        str += "<cite><input type='text' name='page' style='width:25px' title='输入页码,按回车快速跳转' value='" + pageNum + "' onkeydown=\"if(event.keyCode==13){this.form.submit();}\" /><span> / " + pageCount + " 页</span></cite>";
        if (pageNum < pageCount) {
            str += "<a href='" + url.replace("$num$", (pageNum + 1)) + "'>下一页</a>";
        }
        return str;
    }
    else {
        return "";
    }
}
下一篇:没有了