1、js部分(单独保存为page.js)--js中的加减参数需要根据实际情形决定,即table第一行开始到真正的数据行之间有几行,则加减参数就是几
/**
* 分页函数
* pno--页数
* psize--每页显示记录数
* 分页部分是从真实数据行开始,因而存在加减某个常数,以确定真正的记录数
* 纯js分页实质是数据行全部加载,通过是否显示属性完成分页功能
**/
function goPage(pno,psize){
var itable = document.getElementById("idData");
var num = itable.rows.length;//表格行数
var totalPage = 0;//总页数
var pageSize = psize;//每页显示行数
if((num-1)/pageSize > parseInt((num-1)/pageSize)){
totalPage=parseInt((num-1)/pageSize)+1;
}else{
totalPage=parseInt((num-1)/pageSize);
}
var currentPage = pno;//当前页数
var startRow = (currentPage - 1) * pageSize+1;//开始显示的行
var endRow = currentPage * pageSize+1;//结束显示的行
endRow = (endRow > num)? num : endRow;
//前三行始终显示
for(i=0;i<1;i++){
var irow = itable.rows[i];
irow.style.display = "block";
}
for(var i=1;i<num;i++){
var irow = itable.rows[i];
if(i>=startRow&&i<endRow){
irow.style.display = "block";
}else{
irow.style.display = "none";
}
}
var pageEnd = document.getElementById("pageEnd");
var tempStr = "共"+(num-1)+"条记录 分"+totalPage+"页 当前第"+currentPage+"页";
if(currentPage>1){
tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage-1)+","+psize+")\">上一页</a>"
}else{
tempStr += "上一页";
}
if(currentPage<totalPage){
tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage+1)+","+psize+")\">下一页</a>";
}else{
tempStr += "下一页";
}
if(currentPage>1){
tempStr += "<a href=\"#\" onClick=\"goPage("+(1)+","+psize+")\">首页</a>";
}else{
tempStr += "首页";
}
if(currentPage<totalPage){
tempStr += "<a href=\"#\" onClick=\"goPage("+(totalPage)+","+psize+")\">尾页</a>";
}else{
tempStr += "尾页";
}
document.getElementById("barcon").innerHTML = tempStr;
}
2、html部分
<html>
<head>
<script type="text/javascript" src="page.js"></script>
</head>
<body onLoad="goPage(1,10);">
<table id="idData" width="70%">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>户籍</td>
</tr>
<tr>
<td>刘金钟</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong1</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong2</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong3</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong4</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong5</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong6</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong7</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
<tr>
<td>liujinzhong</td>
<td>25</td>
<td>男</td>
<td>山西吕梁</td>
</tr>
</table>
<table width="60%" align="right">
<tr>
<td>
<div id="barcon" name="barcon"></div>
</td>
</td>
</table>
</body>
</html>