var totalcount = parseint($('.totalc').val().trim()), numperpage = parseint($('.numpp').val().trim()), currentpage = parseint($('.curp').val().trim()); var _href = location.href; var key=_href.indexof("?word=")==-1?"?p=":"&p="; if(_href.indexof(key) >= 0){ _href = _href.split(key)[0] } _href = _href + key; (function() { var page = (function() { var page = function(obj) { return fn.init(obj); } var fn = page.prototype = { init: function(obj) { this.pagecount = obj.pagecount; //总页数 if(this.pagecount <= 1){ return false; } this.pagesize = obj.pagesize || 7; //分页大小 一开始最多显示几页 默认7 this.pagenow = currentpage; //当前页 this.containerid = obj.id; //装页码的容器 this.container = document.getelementbyid(this.containerid); fn.drawpage(); return this; }, drawpage: function() { this.container.innerhtml = ""; var front = document.createelement("a"); var next = document.createelement("a"); this.acontainer = document.createelement("span"); front.innerhtml = "< 上一页"; if(currentpage > 1){ front.href = _href+(currentpage-1); } if(currentpage < math.ceil(totalcount/numperpage)){ next.href = _href+(currentpage+1); } if(currentpage ){ } next.innerhtml = "下一页 >" front.classname = "front"; next.classname = "next"; this.container.appendchild(front); //上一页 放在container 中 if(this.pagenow == 1){ front.classname += " disable"; } if(this.pagenow == this.pagecount){ next.classname += " disable"; } this.front = front; this.next = next; if (this.pagecount <= this.pagesize) { for (var i = 0; i < this.pagecount; i++) { var a = document.createelement("a"); a.innerhtml = i + 1; a.href = _href+(i+1); this.acontainer.appendchild(a); //页码放在acontainer 中 if (i + 1 == this.pagenow) a.classname = "select"; //当前页样式为select } } else { /*-------------------------------总页数多余pagesize-------------------*/ var etc2 = document.createelement("b"); etc2.innerhtml = "…"; etc2.classname = "etc"; if (this.pagenow <= this.pagesize - 1) { for (var i = 0; i < this.pagesize; i++) { var a = document.createelement("a"); a.innerhtml = i + 1; a.href = _href+(i+1); this.acontainer.appendchild(a); //页码放在acontainer 中 if (i + 1 == this.pagenow) a.classname = "select"; //当前页样式为select } fn.check(); this.acontainer.appendchild(etc2); } else { fn.reset(); } } var jumpcontainer = document.createelement("span"); //放置页码跳转的容器 jumpcontainer.classname = "p-skip"; var em = document.createelement("em"); var span2 = document.createelement("span"); var inputbox = document.createelement("input"); inputbox.setattribute('value', currentpage); var button = document.createelement("input"); inputbox.type = "text"; button.value = "确定"; button.type = "button"; inputbox.classname = "box"; this.inputbox = inputbox; button.onclick = function() { var page = document.getelementsbyclassname('box')[0].value.trim(); fn.jump(page); } em.innerhtml = "共" + this.pagecount + "页"; span2.innerhtml = "  到第"; em.classname = "jumpcontainer"; span2.classname = "jumpcontainer"; span2.appendchild(inputbox); span2.innerhtml += '页'; span2.appendchild(button); jumpcontainer.appendchild(em); jumpcontainer.appendchild(span2); this.container.appendchild(this.acontainer); //绘制页码 this.container.appendchild(next); //下一页 放在container this.container.appendchild(jumpcontainer); }, jump: function(page) { if (page > this.pagecount) { page = this.pagecount; } if (page < 1) { page = 1; } location.href = _href+page; }, check: function() { if (this.pagenow <= 1) { this.front.classname += " disable"; } else { this.front.classname = "front"; } }, reset: function() { this.acontainer.innerhtml = ""; var a1 = document.createelement("a"); var a2 = document.createelement("a"); var etc1 = document.createelement("b"); var etc2 = document.createelement("b"); a1.innerhtml = "1"; a1.href = _href+1; a2.innerhtml = "2"; a2.href = _href+2; etc1.innerhtml = "…"; etc2.innerhtml = "…"; etc1.classname = etc2.classname = "etc"; this.front.classname = "front"; this.acontainer.appendchild(a1); this.acontainer.appendchild(a2); this.acontainer.appendchild(etc1); var start; /*调整*/ start = this.pagenow - math.ceil((this.pagesize - 2) / 2) + 1; (start >= this.pagecount - (this.pagesize - 2) + 1) ? (start = (this.pagecount - (this.pagesize - 2) + 1), this.isend = true) : (this.isend = false); //起始页码判断 this.start = start; for (var i = 0; i < this.pagesize - 2; i++) { var a = document.createelement("a"); a.innerhtml = start + i; a.href = _href+(start+i); this.acontainer.appendchild(a); //页码放在acontainer 中 if ((start + i) == this.pagenow) a.classname = "select"; } (!this.isend) && this.acontainer.appendchild(etc2); //如果没到最后一组页码显示... (this.pagenow == this.pagecount) && (this.next.classname += " disable"); //到了最后一页不显示下一页 (this.pagenow != this.pagecount) && (this.next.classname = "next"); } } return page; })(); window.page = page; })(); page({ pagecount: math.ceil(totalcount/numperpage), pagesize: 5, pagenow: 1, id: "pagecontrol" });