<div class="codetitle"> <span><a style="CURSOR: pointer" data="13320" class="copybut" id="copybut13320" onclick="doCopy('code13320')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code13320"> <br>/** <br>* Ajax分页功能 <br>* 在需要分页的地方添加<ul class="pagination"> <br>* 作为分页组件容器元素。 <br>* pageCount 总页数 <br>* currentPage 当前页数 <br>* container 带有pagination类的ol容器元素 <br>* loadData 用于加载数据的函数 <br>* version 1.0 <br>*/ <br>pagination : function(pageCount, currentPage, container, loadData) { <br>this.startPage = 1; <br>this.endPage = pageCount; <br>this.minDisplayPageCount = 5; <br>var c = $(container); <br>var paginationLinks = ""; <br>if(pageCount == 1) { <br>c.css({'visibility': 'hidden'}); <br>return; <br>} <br>if(pageCount > this.minDisplayPageCount + 1) { <br>if(currentPage - this.minDisplayPageCount > 0) { <br>this.startPage = currentPage - this.minDisplayPageCount; <br>} <br>if((currentPage + this.minDisplayPageCount - 1) this.endPage = currentPage + this.minDisplayPageCount - 1; <br>} else { <br>this.endPage = pageCount; <br>} <br>} <br>paginationLinks += "<ul>"; <br>if(currentPage != 1) { <br>paginationLinks += "<li><a id="prevpage" href="javascript:;" rel="prev">《上一页</a></li>"; <br>} <br>for(var i = this.startPage; i if(currentPage == i) { <br>paginationLinks += "<li id='page_" + currentPage + "_container'><a id='page_" + i + "' class="current" href="javascript:;">" + currentPage + "</a></li>"; <br>} else { <br>paginationLinks += "<li id='page_" + i + "_container'><a id='page_" + i + "' href="javascript:;">" + i + "</a></li>"; <br>} <br>} <br>if(currentPage paginationLinks += "<li><a id="nextpage" href="javascript:;" rel="next">下一页》</a></li>"; <br>} <br>paginationLinks += "</ul>"; <br>c.html(paginationLinks); <br>var links = $("#page_number ul li a"); <br>links.each(function(index) { <br>if(!(this.innerHTML == "上一页" || this.innerHTML == "下一页")) { <br>$(this).click(function(event) { <br>alert(links[index].innerHTML); <br>loadData(curTaskId,"","",parseInt(links[index].innerHTML)); <br>pagination(pageCount, parseInt(links[index].innerHTML), container, loadData); <br>}); <br>} <br>}); <br>var prevPage = $("#prevpage"); <br>var nextPage = $("#nextpage"); <br>c.css({'visibility': 'visible'}); <br>if(prevPage) { <br>prevPage.click(function(event) { <br>loadData(curTaskId,"","",currentPage - 1); <br>pagination(pageCount, currentPage - 1, container, loadData); <br>}); <br>} <br>if(nextPage) { <br>nextPage.click(function(event) { <br>loadData(curTaskId,"","",currentPage + 1); <br>pagination(pageCount, currentPage + 1, container, loadData); <br>}); <br>} <br>} <br> </ul> </div> <br>loadData为加载数据的函数,这个函数需要定义一个当前页数的参数,比如: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="5034" class="copybut" id="copybut5034" onclick="doCopy('code5034')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code5034"> <br>var currentPage = 1; <br>loadExamList(currentPage){ <br>//TODO <br>pagination(5,currentPage,$(ul),loadExamList); <br>}; <br> </div> <br>5是总页数,1是当前页数,$(ul)是要将页码按钮存放的位置,loadExamList是点击上一页、下一页或者页码的时候调用的函数。