-
Notifications
You must be signed in to change notification settings - Fork 26
/
js分页.html
151 lines (129 loc) · 4.38 KB
/
js分页.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>js分页demo</title>
<style type="text/css">
a{
margin:5px;
}
</style>
</head>
<body>
<div id="block">
<!-- <a href="#">首页</a>
<a href="#">上一页</a>
<a href="#">[1]</a>
<a href="#">[2]</a>
<a href="#">3</a>
<a href="#">[4]</a>
<a href="#">[5]</a>
<a href="#">下一页</a>
<a href="#">尾页</a> -->
</div>
<script>
window.onload = function(){
page({
obj : 'block',
nowNum : 7,
allNum : 10,
callback : pageCall
});
}
function pageCall(now,all){
alert('当前页:'+now+'; 总共页:'+all);
}
function page(opt){
var div = document.getElementById(opt.obj);
var nowNum = opt.nowNum || 1;
var allNum = opt.allNum || 5;
var pageCall = opt.callback || function(){};
if(nowNum>=4&&allNum>=6){
var oA = document.createElement('a');
oA.href = '#1';
oA.innerHTML = '首页';
div.appendChild(oA);
}
if(nowNum>=2){
var oA = document.createElement('a');
oA.href = '#'+(nowNum-1);
oA.innerHTML = '上一页';
div.appendChild(oA);
}
if(allNum<=5){
for(var i=1;i<=allNum;i++){
var oA = document.createElement('a');
oA.href='#'+i;
if(i == nowNum){
oA.innerHTML = i;
}else{
oA.innerHTML = '['+i+']';
}
div.appendChild(oA);
}
}else{
for(var i=1;i<=5;i++){
var oA = document.createElement('a');
//当前页为1和2的时候不应该前面出现-1和0
if(nowNum==1||nowNum==2){
oA.href = '#'+i;
if(nowNum ==i){
oA.innerHTML =i;
}else{
oA.innerHTML ='['+i+']';
}
//当前页为总数的后两页时,不应该有超过总数的页数
}else if((allNum - nowNum) == 0 || (allNum - nowNum) == 1){
oA.href = '#'+ (allNum-5+i);
if((allNum - nowNum) == 0&&i==5){
oA.innerHTML = (allNum-5+i);
}else if((allNum - nowNum) == 1&&i==4){
oA.innerHTML = (allNum-5+i);
}else{
oA.innerHTML = '['+(allNum-5+i)+']';
}
//显示当前页前后共5页
}else{
//第几次循环#后的数字为几
oA.href='#'+(nowNum-3+i);
oA.innerHTML = '['+(nowNum-3+i)+']';
//当前页不应该有中括号
if(i==3){
oA.innerHTML = (nowNum-3+i);
}
}
div.appendChild(oA);
}
}
if((allNum - nowNum)>=1){
var oA = document.createElement('a');
oA.href = '#'+(nowNum + 1);
oA.innerHTML = '下一页';
div.appendChild(oA);
}
if((allNum - nowNum)>=3 && allNum >=6){
var oA = document.createElement('a');
oA.href = '#'+allNum;
oA.innerHTML = '尾页';
div.appendChild(oA);
}
//回调函数
pageCall(nowNum,allNum);
var oA = div.getElementsByTagName('a');
for(var i=0;i<oA.length;i++){
oA[i].onclick = function(){
var nowNum = parseInt(this.getAttribute('href').substring(1),10);
div.innerHTML = '';
page({
obj : opt.obj,
nowNum : nowNum,
allNum : allNum,
callback : pageCall
});
return false;
}
}
}
</script>
</body>
</html>