JavaScript EasyPager 分页函数_javascript技巧
程序员文章站
2022-06-13 22:28:43
...
复制代码 代码如下:
var EasyPager = function(wrap,pageCount,currPage){
var d = document
,el = function(tag,inner,click,css){
tag = d.createElement(tag);
inner && (tag.innerHTML = inner);
click && (tag.onclick = click);
css && (tag.className=css);
return tag;
}
,append = function(parent,children,child){
children = [].concat(children);
for(var i=0,j=children.length;i
child && parent.appendChild(child);
}
return parent;
}
,easyFn = function(n){return function(){EasyPager(wrap,pageCount,n)}}
,box = el("div"),firstPage,prevPage,lastPage,nextPage,startNum,endNum,pages= [];
currPage!=1 && (firstPage = el("span","currPage!=pageCount && (lastPage = el("span",">>",easyFn(pageCount)),nextPage = el("span",">",easyFn(currPage+1)));
(pageCount=pageCount-5)&&(endNum=pageCount,startNum=pageCount-10+1));
for(var i=startNum;i
append((wrap.innerHTML="",wrap),append(box,[firstPage,prevPage].concat(pages).concat(nextPage,lastPage)));
};