Javascript 分页 完美封装

来源:互联网 发布:淘宝指数排行榜 编辑:程序博客网 时间:2024/05/26 08:41

首先上效果图:


以下为封装好的 方法:


function Paging(pageNum,pageSize,totalCount,skipCount,fuctionName,currentStyleName,currentUseLink,preText,nextText,firstText,lastText){    var returnValue = "";    var begin = 1;    var end = 1;    var totalpage = Math.floor(totalCount / pageSize);    if(totalCount % pageSize >0){        totalpage ++;    }       if(preText == null){        firstText = "prev";    }    if(nextText == null){        nextText = "next";    }        begin = pageNum - skipCount;    end = pageNum + skipCount;        if(begin <= 0){        end = end - begin +1;        begin = 1;    }        if(end > totalpage){        end = totalpage;    }    for(count = begin;count <= end;count ++){        if(currentUseLink){             if(count == pageNum){                returnValue += "<a class=\""+currentStyleName+"\" href=\"javascript:void(0);\" onclick=\""+fuctionName+"("+count.toString()+");\">"+count.toString()+"</a> ";            }            else{                returnValue += "<a href=\"javascript:void(0);\" onclick=\"" + fuctionName + "(" + count.toString() + ");\">" + count.toString() + "</a> ";            }        }        else {            if (count == pageNum) {                returnValue += "<span class=\""+currentStyleName+"\">"+count.toString()+"</span> ";            }            else{                           returnValue += "<a href=\"javascript:void(0);\" onclick=\""+fuctionName+"("+count.toString()+");\">"+count.toString()+"</a> ";}            }        }        if(pageNum - skipCount >1){            returnValue = " ... "+returnValue;        }        if(pageNum + skipCount < totalpage){            returnValue = returnValue + " ... ";        }                if(pageNum > 1){            returnValue = "<a href=\"javascript:void(0);\" onclick=\""+fuctionName+"("+(pageNum - 1).toString()+");\"> " + preText + "</a> " + returnValue;        }        if(pageNum < totalpage){            returnValue = returnValue + " <a href=\"javascript:void(0);\" onclick=\""+fuctionName+"("+(pageNum+1).toString()+");\">" + nextText + "</a>";        }                if(firstText!= null){            if(pageNum >1){                returnValue = "<a href=\"javascript:void(0);\" onclick=\""+fuctionName+"(1);\">" + firstText + "</a> " + returnValue;}        }        if(lastText !=null){            if(pageNum < totalpage){                returnValue = returnValue + " " + " <a href=\"javascript:void(0);\" onclick=\""+fuctionName+"("+totalpage.toString()+");\">" + lastText + "</a>";}        }        return returnValue;        }

原创粉丝点击