用javascript中的箭头分页

时间:2015-04-09 23:14:28

标签: javascript

这是我的javascript代码:

function setPage(currentPage){
    currentPage = parseInt(currentPage) + 1;
    var last = JSON.parse(localStorage.getItem("Attempt"));
    var lastPage = last.length - 1;
    var prevcurPage = currentPage - 1;
    var prevcurPage2 = currentPage - 2;
    var nextcurPage = currentPage + 1;
    var nextcurPage2 = currentPage + 2;
    var content = "";
    if(lastPage == 1){

    }else{
    if(currentPage == 1){
        if(lastPage > 2){
            content+=" [<a href=''>"+currentPage+"</a>] | ";
            content+=" <a href=''>"+nextcurPage+"</a> | ";
            content+=" <a href=''>"+nextcurPage2+"</a> | ";
        }else{
            content+=" [<a href=''>"+currentPage+"</a>] | ";
            content+=" <a href=''>"+nextcurPage+"</a> | ";
        }

    }else if(currentPage == lastPage){
        if(lastPage > 2){
            content+=" <a href=''>"+prevcurPage2+"</a> | ";
            content+=" <a href=''>"+prevcurPage+"</a> | ";
            content+=" [<a href=''>"+currentPage+"</a>] | ";
        }else{
            content+=" <a href=''>"+prevcurPage+"</a> | ";
            content+=" [<a href=''>"+currentPage+"</a>] | ";
        }
    }else{
            content+=" <a href=''>"+prevcurPage+"</a> | ";
            content+=" [<a href=''>"+currentPage+"</a>] | ";
            content+=" <a href=''>"+nextcurPage+"</a> | ";
    }
}

currentPage以0开头;所以我需要添加1; 为什么没有显示正确的分页?我怎么用箭头?

我希望这个展示,

currentpage = 0; [1] | 2 | 3 >
currentpage = 2; 1 | 2 | [3] | 4 | 5 >
currentpage = 7; < 4 | 5 | [6]

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

if (currentPage > 3)
    content += " < ";
content += currentPage - 2 > 0 ? "<a href=''>"+(currentPage - 2)+"</a> | " : '';
content += currentPage - 1 > 0 ? "<a href=''>"+(currentPage - 1)+"</a> | " : '';

content += "[" + currentPage + "]";

content += currentPage + 1 <= lastPage ? " | <a href=''>"+(currentPage + 1)+"</a>" : '';
content += currentPage + 2 <= lastPage ? " | <a href=''>"+(currentPage + 2)+"</a>" : '';
if (currentPage < lastPage - 2)
    content += " > ";

或者为了更好的灵活性,请使用某种范围变量:

var range = 2;

if (currentPage > range + 1)
    content += " < ";

for (var a = range; a > 0; a--)
    content += currentPage - a > 0 ? "<a href=''>"+(currentPage - a)+"</a> | " : '';

content += "[" + currentPage + "]";

for (var a = 1; a <= range; a++)
    content += currentPage + a <= lastPage ? " | <a href=''>"+(currentPage + a)+"</a>" : '';

if (currentPage < lastPage - range)
    content += " > ";