1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>JS分页</title>
6 <style type="text/css">
7 body{font-size:12px;}
8 #pager{ height:20px; float:left;}
9 #pager a{height:18px; line-height:18px; border:1px solid #ccc; padding-left:0.4em; padding-right:0.4em; text-decoration:none; display:block; float:left; margin:0px 3px;}
10 #pager a:hover{background-color:#1f3a87; color:#fff; text-decoration:none;}
11 .not-current{background-color:#fff; color:#1f3a87;}
12 .current{background-color:#1f3a87; color:#fff; text-decoration:none;}
13 </style>
14 </head>
15
16 <body onload="CreatePager(10,10,2043,'?page=14',10)">
17 <script type="text/javascript">
18 //pageSize,每页显示多少条记录,currentPage当前页,recordCount共有多少条记录,url页面路径,必须带有page参数,numCount,每页显示多少个分页数字
19 var CreatePager=function(pageSize,currentPage,recordCount,url,numCount){
20 //创建一个链接
21 var CreateHref=function(url,page,p_ele,t,currentPage){
22 var a=document.createElement('a');
23 var reg=new RegExp("page=\\d+");
24 url=url.replace(reg.exec(url),'page='+page)
25 a.setAttribute('href',url);
26 a.setAttribute('class',currentPage==page?'current':'not-current');
27 var t=document.createTextNode(t);
28 a.appendChild(t);
29 document.getElementById(p_ele).appendChild(a);
30 }
31 //计算属于哪个区间
32 var GetNumArea=function(num,numCount,pageCount){
33 var currentArea=parseInt((num-1)/numCount)+1;
34 var lastArea=parseInt((pageCount-1)/numCount)+1;
35 this.minNum=(currentArea-1)*numCount+1;
36 var tempTaxNumber=currentArea*numCount+1;
37 this.maxNum=tempTaxNumber>pageCount?pageCount:tempTaxNumber;
38 this.preAreaNum=currentArea<=1?1:this.minNum-1;
39 this.afterAreaNum=currentArea>=lastArea?pageCount:this.maxNum+1;
40 }
41 var pageCount=0;//总页数
42 pageCount=recordCount%pageSize===0?recordCount/pageSize:parseInt(recordCount/pageSize)+1;//总页数
43
44 // var pager=document.getElementById('pager');//获取分页的元素
45 // var pageSummary=document.createElement('span');//创建一个DIV,在里面显示第几页共几页
46 // var summaryContent=document.createTextNode('第'+currentPage+'页共'+pageCount+'页');
47 // pageSummary.appendChild(summaryContent);
48 // pager.appendChild(pageSummary);
49
50 CreateHref(url,1,'pager','首页',currentPage);//添加首页链接
51 CreateHref(url,new GetNumArea(currentPage,numCount,pageCount).preAreaNum,'pager','<<',currentPage);//添加前面的省略号
52 //显示分页数字 开始
53 var pageArea = pageCount / numCount;// 页码区间
54 if(pageCount<numCount){
55 for(i=1;i<=pageCount;i++){
56 CreateHref(url,i,'pager',i,currentPage);
57 }
58 }else{
59 currentPage=currentPage>=pageCount?pageCount:currentPage;
60 var minNum=new GetNumArea(currentPage,numCount,pageCount).minNum;
61 var maxNum=new GetNumArea(currentPage,numCount,pageCount).maxNum;
62 for(i=minNum;i<maxNum;i++){
63 CreateHref(url,i,'pager',i,currentPage);
64 }
65 }
66
67 //显示分页数字 结束
68 CreateHref(url,new GetNumArea(currentPage,numCount,pageCount).afterAreaNum,'pager','>>',currentPage);//显示分页后面的省略号
69 CreateHref(url,pageCount,'pager','末页',currentPage);//添加末页链
70 }
71 </script>
72
73 <div id='pager'></div>
74 </body>
75 </html>
76