一.数据分页
1.创建页码
1.使用if、else语句对页码进行前缩、后缩以及前后缩;并进行传值;
if (i == 2 && current - 5 > i) {
i = current - 1;
str += “
- …
- ”;
}
else if (i == current + 5 && current + 5 < totle) {
i = totle - 1;
str += “ - …
- ”;
}
else {
if (current == i) {
str += “ - ” + i + “
- ”;
}
else {
str += “ - ” + i + “
- ”;
}
}
}
2.使用onclick事件对页码进行鼠标操控跳转以及上、下一页的跳转
上一页:
his.back.onclick = function () {
that.pageNow–;
if (that.pageNow < 1) {
that.pageNow = 1;
}
that.createli(that.pageNow, that.pagetotleNum);
that.getliNumAddEvent();
that.getNowData(that.pageNow);
that.createNowDataEle();
}
下一页:
this.advance.onclick = function () {
that.pageNow++;
if (that.pageNow > that.pagetotleNum) {
that.pageNow = that.pagetotleNum;
}
that.createli(that.pageNow, that.pagetotleNum);
that.getliNumAddEvent();
that.getNowData(that.pageNow);
that.createNowDataEle();
}
点击数据页码跳转
getliNumAddEvent: function () {
var that = this;
var lilist = document.getElementsByClassName(“linum”);
for (var i = 0; i < lilist.length; i++) {
lilist[i].onclick = function () {
Paging.pageNow = parseInt(this.innerHTML);
Paging.createli(Paging.pageNow, Paging.pagetotleNum);
Paging.getliNumAddEvent();
that.getNowData(that.pageNow);
that.createNowDataEle();
}
}
},
3.在当前页创建并加载数据
createNowDataEle: function () {
this.newslist.innerHTML=""
for (var i = 0; i < this.pageNum; i++) {
var div = document.createElement(“div”);
div.className = “divlist”;
使用key做键进行数据的传值
for (var key in this.pageNowData[i]) {
var childdiv = document.createElement(“div”);
childdiv.className = “childdiv”;
childdiv.innerHTML = this.pageNowData[i][key];
div.appendChild(childdiv);
}
this.newslist.appendChild(div);
}
},