用js实现分页效果
Posted 秋水涴晴汐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用js实现分页效果相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#data {color: red;border: solid;text-align: center;}
a{text-decoration: none;}
</style>
</head>
<body onLoad="Page(1,10);">
<table id="data" width="70%">
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
<tr><td>aaaaaaaaaaaa</td><td>1111111</td><td>吖吖</td><td>呵呵</td></tr>
</table>
<table width="60%" align="right">
<tr><td><div id="con" name="con"></div></td></tr>
</table>
<script>
//原理:所有数据已加载好,js通过遍历部分显示,实现分页效果
html代码
// 分页函数
// pno--页数
// size--每页显示记录数
// 分页部分是从真实数据行开始,因而存在加减某个常数,以确定真正的记录数
function Page(pno,size){
var data = document.getElementById("data");
var num = data.rows.length;//表格所有行数(所有记录数)
console.log(num);
var totalPage = 0;//总页数
var pageSize = size;//每页显示行数
//总共分几页
if(num/pageSize > parseInt(num/pageSize)){
totalPage=parseInt(num/pageSize)+1;
}else{
totalPage=parseInt(num/pageSize);
}
//当前页数
var currentPage = pno;
var startRow = (currentPage - 1) * pageSize+1;//开始显示的行
var endRow = currentPage * pageSize;//结束显示的行
endRow = (endRow > num)? num : endRow; //三目运算符,
console.log(endRow);//控制台打印出结束的行
//遍历显示数据实现分页
for(var i=1;i<(num+1);i++){
var irow = data.rows[i-1];
if(i>=startRow && i<=endRow){
irow.style.display = "block";
}else{
irow.style.display = "none";
}
}
var pageEnd = document.getElementById("pageEnd");
var tempStr = "共"+num+"条记录 分"+totalPage+"页 当前第"+currentPage+"页";
//判断页数>1时
if(currentPage>1){
tempStr += "<a href=\"#\" onClick=\"Page("+(1)+","+size+")\">首页</a>";
tempStr += "<a href=\"#\" onClick=\"Page("+(currentPage-1)+","+size+")\"><上一页</a>"
}else{
tempStr += "首页";
tempStr += "<上一页";
}
//判断页数<总页数时
if(currentPage<totalPage){
tempStr += "<a href=\"#\" onClick=\"Page("+(currentPage+1)+","+size+")\">下一页></a>";
tempStr += "<a href=\"#\" onClick=\"Page("+(totalPage)+","+size+")\">尾页</a>";
}else{
tempStr += "下一页>";
tempStr += "尾页";
}
document.getElementById("con").innerHTML = tempStr;
}
</script>
</body>
</html>
以上是关于用js实现分页效果的主要内容,如果未能解决你的问题,请参考以下文章