DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 心揚JS分頁函數代碼
心揚JS分頁函數代碼
編輯:關於JavaScript     
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS分頁</title>
<style type="text/css">
body{font-size:12px;}
#pager{ height:20px; float:left;}
#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;}
#pager a:hover{background-color:#1f3a87; color:#fff; text-decoration:none;}
.not-current{background-color:#fff; color:#1f3a87;}
.current{background-color:#1f3a87; color:#fff; text-decoration:none;}
</style>
</head>
<body onload="CreatePager(10,10,2043,'?page=14',10)">
<script type="text/javascript">
//pageSize,每頁顯示多少條記錄,currentPage當前頁,recordCount共有多少條記錄,url頁面路徑,必須帶有page參數,numCount,每頁顯示多少個分頁數字
var CreatePager=function(pageSize,currentPage,recordCount,url,numCount){
//創建一個鏈接
var CreateHref=function(url,page,p_ele,t,currentPage){
var a=document.createElement('a');
var reg=new RegExp("page=\\d+");
url=url.replace(reg.exec(url),'page='+page)
a.setAttribute('href',url);
a.setAttribute('class',currentPage==page?'current':'not-current');
var t=document.createTextNode(t);
a.appendChild(t);
document.getElementById(p_ele).appendChild(a);
}
//計算屬於哪個區間
var GetNumArea=function(num,numCount,pageCount){
var currentArea=parseInt((num-1)/numCount)+1;
var lastArea=parseInt((pageCount-1)/numCount)+1;
this.minNum=(currentArea-1)*numCount+1;
var tempTaxNumber=currentArea*numCount+1;
this.maxNum=tempTaxNumber>pageCount?pageCount:tempTaxNumber;
this.preAreaNum=currentArea<=1?1:this.minNum-1;
this.afterAreaNum=currentArea>=lastArea?pageCount:this.maxNum+1;
}
var pageCount=0;//總頁數
pageCount=recordCount%pageSize===0?recordCount/pageSize:parseInt(recordCount/pageSize)+1;//總頁數
// var pager=document.getElementById('pager');//獲取分頁的元素
// var pageSummary=document.createElement('span');//創建一個DIV,在裡面顯示第幾頁共幾頁
// var summaryContent=document.createTextNode('第'+currentPage+'頁共'+pageCount+'頁');
// pageSummary.appendChild(summaryContent);
// pager.appendChild(pageSummary);
CreateHref(url,1,'pager','首頁',currentPage);//添加首頁鏈接
CreateHref(url,new GetNumArea(currentPage,numCount,pageCount).preAreaNum,'pager','<<',currentPage);//添加前面的省略號
//顯示分頁數字 開始
var pageArea = pageCount / numCount;// 頁碼區間
if(pageCount<numCount){
for(i=1;i<=pageCount;i++){
CreateHref(url,i,'pager',i,currentPage);
}
}else{
currentPage=currentPage>=pageCount?pageCount:currentPage;
var minNum=new GetNumArea(currentPage,numCount,pageCount).minNum;
var maxNum=new GetNumArea(currentPage,numCount,pageCount).maxNum;
for(i=minNum;i<maxNum;i++){
CreateHref(url,i,'pager',i,currentPage);
}
}
//顯示分頁數字 結束
CreateHref(url,new GetNumArea(currentPage,numCount,pageCount).afterAreaNum,'pager','>>',currentPage);//顯示分頁後面的省略號
CreateHref(url,pageCount,'pager','末頁',currentPage);//添加末頁鏈
}
</script>
<div id='pager'></div>
</body>
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved