首页 > 代码库 > js 分页
js 分页
分页JS代码
<style></style><!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分页JS代码</title>
<style type="text/css">
.page{margin:2em;}
.page a{text-decoration:none;display:inline-block;line-height:14px;padding:2px 5px;color:#333;border:1px solid #ccc;margin:0 2px;}
.page a:hover,.page a.on{background:#999;color:#fff;border:1px solid #333;}
.page a.unclick,.page a.unclick:hover{background:none;border:1px solid #eee;color:#999;cursor:default;}
</style>
</head>
<body>
<div class="page"></div>
<div class="page"></div>
</body>
<script type="text/javascript">
//container 容器,count 总页数 pageindex 当前页数
function setPage(container, count, pageindex) {
var container = container;
var count = count;
var pageindex = pageindex;
var a = [];
//总页数少于10 全部显示,大于10 显示前3 后3 中间3 其余....
if (pageindex == 1) {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"prev unclick\">prev</a>";
} else {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"prev\">prev</a>";
}
function setPageList() {
if (pageindex == i) {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"on\">" + i + "</a>";
} else {
a[a.length] = "<a href=http://www.mamicode.com/"#\">" + i + "</a>";
}
}
//总页数小于10
if (count <= 10) {
for (var i = 1; i <= count; i++) {
setPageList();
}
}
//总页数大于10页
else {
if (pageindex <= 4) {
for (var i = 1; i <= 5; i++) {
setPageList();
}
a[a.length] = "...<a href=http://www.mamicode.com/"#\">" + count + "</a>";
} else if (pageindex >= count - 3) {
a[a.length] = "<a href=http://www.mamicode.com/"#\">1</a>...";
for (var i = count - 4; i <= count; i++) {
setPageList();
}
}
else { //当前页在中间部分
a[a.length] = "<a href=http://www.mamicode.com/"#\">1</a>...";
for (var i = pageindex - 2; i <= pageindex + 2; i++) {
setPageList();
}
a[a.length] = "...<a href=http://www.mamicode.com/"#\">" + count + "</a>";
}
}
if (pageindex == count) {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"next unclick\">next</a>";
} else {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"next\">next</a>";
}
container.innerHTML = a.join("");
//事件点击
var pageClick = function() {
var oAlink = container.getElementsByTagName("a");
var inx = pageindex; //初始的页码
oAlink[0].onclick = function() { //点击上一页
if (inx == 1) {
return false;
}
inx--;
setPage(container, count, inx);
return false;
}
for (var i = 1; i < oAlink.length - 1; i++) { //点击页码
oAlink[i].onclick = function() {
inx = parseInt(this.innerHTML);
setPage(container, count, inx);
return false;
}
}
oAlink[oAlink.length - 1].onclick = function() { //点击下一页
if (inx == count) {
return false;
}
inx++;
setPage(container, count, inx);
return false;
}
} ()
}
setPage(document.getElementsByTagName("div")[0],10,1);
setPage(document.getElementsByTagName("div")[1],13,5);
</script>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分页JS代码</title>
<style type="text/css">
.page{margin:2em;}
.page a{text-decoration:none;display:inline-block;line-height:14px;padding:2px 5px;color:#333;border:1px solid #ccc;margin:0 2px;}
.page a:hover,.page a.on{background:#999;color:#fff;border:1px solid #333;}
.page a.unclick,.page a.unclick:hover{background:none;border:1px solid #eee;color:#999;cursor:default;}
</style>
</head>
<body>
<div class="page"></div>
<div class="page"></div>
</body>
<script type="text/javascript">
//container 容器,count 总页数 pageindex 当前页数
function setPage(container, count, pageindex) {
var container = container;
var count = count;
var pageindex = pageindex;
var a = [];
//总页数少于10 全部显示,大于10 显示前3 后3 中间3 其余....
if (pageindex == 1) {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"prev unclick\">prev</a>";
} else {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"prev\">prev</a>";
}
function setPageList() {
if (pageindex == i) {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"on\">" + i + "</a>";
} else {
a[a.length] = "<a href=http://www.mamicode.com/"#\">" + i + "</a>";
}
}
//总页数小于10
if (count <= 10) {
for (var i = 1; i <= count; i++) {
setPageList();
}
}
//总页数大于10页
else {
if (pageindex <= 4) {
for (var i = 1; i <= 5; i++) {
setPageList();
}
a[a.length] = "...<a href=http://www.mamicode.com/"#\">" + count + "</a>";
} else if (pageindex >= count - 3) {
a[a.length] = "<a href=http://www.mamicode.com/"#\">1</a>...";
for (var i = count - 4; i <= count; i++) {
setPageList();
}
}
else { //当前页在中间部分
a[a.length] = "<a href=http://www.mamicode.com/"#\">1</a>...";
for (var i = pageindex - 2; i <= pageindex + 2; i++) {
setPageList();
}
a[a.length] = "...<a href=http://www.mamicode.com/"#\">" + count + "</a>";
}
}
if (pageindex == count) {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"next unclick\">next</a>";
} else {
a[a.length] = "<a href=http://www.mamicode.com/"#\" class=\"next\">next</a>";
}
container.innerHTML = a.join("");
//事件点击
var pageClick = function() {
var oAlink = container.getElementsByTagName("a");
var inx = pageindex; //初始的页码
oAlink[0].onclick = function() { //点击上一页
if (inx == 1) {
return false;
}
inx--;
setPage(container, count, inx);
return false;
}
for (var i = 1; i < oAlink.length - 1; i++) { //点击页码
oAlink[i].onclick = function() {
inx = parseInt(this.innerHTML);
setPage(container, count, inx);
return false;
}
}
oAlink[oAlink.length - 1].onclick = function() { //点击下一页
if (inx == count) {
return false;
}
inx++;
setPage(container, count, inx);
return false;
}
} ()
}
setPage(document.getElementsByTagName("div")[0],10,1);
setPage(document.getElementsByTagName("div")[1],13,5);
</script>
</html>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。