---------------------JQ版本----------------------------
<script type="text/javascript">
var pageSize = 15; //每页显示的记录条数var curPage=0; //当前页
var lastPage; //最后页
var direct=0; //方向
var len; //总行数
var page; //总页数
var begin;
var end;
$(document).ready(function display(){
len =$("#mytable tr").length - 1; // 求这个表的总行数,剔除第一行介绍
page=len % pageSize==0 ? len/pageSize : Math.floor(len/pageSize)+1;//根据记录条数,计算页数
alert("page==="+len);
curPage=1; // 设置当前为第一页
displayPage(1);//显示第一页
document.getElementById("btn0").innerHTML="当前 " + curPage + "/" + page + " 页 每页 "; // 显示当前多少页
document.getElementById("sjzl").innerHTML="数据总量 " + len + ""; // 显示数据量
document.getElementById("pageSize").value = pageSize;
function firstPage(){ // 首页
curPage=1;
direct = 0;
displayPage();
}
$("#btn1").click( firstPage());
$("#btn2").click(function frontPage(){ // 上一页
direct=-1;
displayPage();
});
$("#btn3").click(function nextPage(){ // 下一页
direct=1;
displayPage();
});
$("#btn4").click(function lastPage(){ // 尾页
curPage=page;
direct = 0;
displayPage();
});
$("#btn5").click(function changePage(){ // 转页
curPage=document.getElementById("changePage").value * 1;
if (!/^[1-9]\d*$/.test(curPage)) {
alert("请输入正整数");
return ;
}
if (curPage > page) {
alert("超出数据页面");
return ;
}
direct = 0;
displayPage();
});
$("#pageSizeSet").click(function setPageSize(){ // 设置每页显示多少条记录
pageSize = document.getElementById("pageSize").value; //每页显示的记录条数
if (!/^[1-9]\d*$/.test(pageSize)) {
alert("请输入正整数");
return ;
}
len =$("#mytable tr").length - 1;
page=len % pageSize==0 ? len/pageSize : Math.floor(len/pageSize)+1;//根据记录条数,计算页数
curPage=1; //当前页
direct=0; //方向
firstPage();
});
});
function displayPage(){
if(curPage <=1 && direct==-1){
direct=0;
alert("已经是第一页了");
return;
} else if (curPage >= page && direct==1) {
direct=0;
alert("已经是最后一页了");
return ;
}
lastPage = curPage;
// 修复当len=1时,curPage计算得0的bug
if (len > pageSize) {
curPage = ((curPage + direct + len) % len);
} else {
curPage = 1;
}
document.getElementById("btn0").innerHTML="当前 " + curPage + "/" + page + " 页 每页 "; // 显示当前多少页
begin=(curPage-1)*pageSize + 1;// 起始记录号
end = begin + 1*pageSize - 1; // 末尾记录号
if(end > len ) end=len;
$("#mytable tr").hide(); // 首先,设置这行隐藏
$("#mytable tr").each(function(i){ // 然后,通过条件判断决定本行是否恢复显示
if((i>=begin && i<=end) || i==0 )//显示begin<=x<=end的记录
$(this).show();
});
}
</script>
------------------------------------------------普通版本---------------------------------------------------------------
<script>
// JavaScript Document By lishewen
var theTable = document.getElementById("tablelsw");
var totalPage = document.getElementById("spanTotalPage");
var pageNum = document.getElementById("spanPageNum");
var spanPre = document.getElementById("spanPre");
var spanNext = document.getElementById("spanNext");
var spanFirst = document.getElementById("spanFirst");
var spanLast = document.getElementById("spanLast");
var numberRowsInTable = theTable.rows.length;
var pageSize = 4;
var page = 1;
//下一页
function next(){
hideTable();
currentRow = pageSize * page;
maxRow = currentRow + pageSize;
if ( maxRow > numberRowsInTable ) maxRow = numberRowsInTable;
for ( var i = currentRow; i< maxRow; i++ ){
theTable.rows[i].style.display = '';
}
page++;
if ( maxRow == numberRowsInTable ) { nextText(); lastText(); }
showPage();
preLink();
firstLink();
}
//上一页
function pre(){
hideTable();
page--;
currentRow = pageSize * page;
maxRow = currentRow - pageSize;
if ( currentRow > numberRowsInTable ) currentRow = numberRowsInTable;
for ( var i = maxRow; i< currentRow; i++ ){
theTable.rows[i].style.display = '';
}
if ( maxRow == 0 ){ preText(); firstText(); }
showPage();
nextLink();
lastLink();
}
//第一页
function first(){
hideTable();
page = 1;
for ( var i = 0; i<pageSize; i++ ){
theTable.rows[i].style.display = '';
}
showPage();
preText();
nextLink();
lastLink();
}
//最后一页
function last(){
hideTable();
page = pageCount();
currentRow = pageSize * (page - 1);
for ( var i = currentRow; i<numberRowsInTable; i++ ){
theTable.rows[i].style.display = '';
}
showPage();
preLink();
nextText();
firstLink();
}
function hideTable(){
for ( var i = 0; i<numberRowsInTable; i++ ){
theTable.rows[i].style.display = 'none';
}
}
function showPage(){
pageNum.innerHTML = page;
}
//总共页数
function pageCount(){
var count = 0;
if ( numberRowsInTable%pageSize != 0 ) count = 1;
return parseInt(numberRowsInTable/pageSize) + count;
}
//显示链接
function preLink(){ spanPre.innerHTML = "<a href='javascript:pre();'>上一页</a>";}
function preText(){ spanPre.innerHTML = "上一页";}
function nextLink(){ spanNext.innerHTML = "<a href='javascript:next();'>下一页</a>";}
function nextText(){ spanNext.innerHTML = "下一页";}
function firstLink(){ spanFirst.innerHTML = "<a href='javascript:first();'>首页</a>";}
function firstText(){ spanFirst.innerHTML = "首页";}
function lastLink(){ spanLast.innerHTML = "<a href='javascript:last();'>尾页</a>";}
function lastText(){ spanLast.innerHTML = "尾页";}
//隐藏表格
function hide(){
for ( var i = pageSize; i<numberRowsInTable; i++ ){
theTable.rows[i].style.display = 'none';
}
totalPage.innerHTML = pageCount();
pageNum.innerHTML = '1';
nextLink();
lastLink();
}
hide();
</script>