调用方法如下: var p = new Pager(5, 10); p.init('页码所在的容器ID', dataOp); function dataOp() { // ajax数据操作 SendContent("/Admin/TopicService.asmx/GetTopicList?currentPageIndex=" + p.currentPage + "&pageSize=" + p.pageSize, "GET", "", ajax postback method); } JS代码如下: // ============================================================================== // Created by Bndy at 2010/3/18 // Copyright (c) 2010 ahdzlc, All rights reserved. // // * * * * * * * * * * * * * * * * // * Q Q : 8 1 7 9 5 7 0 5 * // * M S N : bndy533@msn.com * // * Email : bndy533@163.com * // * * * * * * * * * * * * * * * * // // ------------------------------------------------------------------------------ // JS 分页函数 // 适合Ajax分页时使用 // ============================================================================== var pager; var handler; var Pager = function(totalRecordCount, pageSize) { this.pageSize = pageSize; this.currentPage = 1; this.totalPageCount = totalRecordCount % pageSize != 0 ? Math.floor(totalRecordCount / pageSize) + 1 : Math.floor(totalRecordCount / pageSize); var ele; this.getBeginPageNum = function() { if (this.pageSize > this.totalPageCount) { return 1; } if (this.currentPage > this.totalPageCount) { return this.totalPageCount - this.pageSize + 1; } else { return Math.floor((this.currentPage - 1) / this.pageSize) * this.pageSize + 1; } }; this.getEndPageNum = function() { var x = Math.floor((this.currentPage - 1) / this.pageSize) * this.pageSize + this.pageSize; if (this.pageSize > this.totalPageCount || this.currentPage > this.totalPageCount || x > this.totalPageCount) { return this.totalPageCount; } return x; }; this.init = function(htmlElementName, handlerMethod) { pager = this; ele = htmlElementName; handler = handlerMethod; var beginPageNum = this.getBeginPageNum(); var endPageNum = this.getEndPageNum(); var str = ""; if (this.currentPage > this.pageSize) { str = " <span οnclick='pager.changePage(" + (beginPageNum - 1) + ")' style="/" mce_style="/""cursor:pointer;/"> << </span> "; } for (var i = beginPageNum; i <= endPageNum; i++) { if (i == this.currentPage) { str = str + " " + i.toString() + " "; } else { str = str + " <span οnclick='pager.changePage(" + i + ")' style="/" mce_style="/""cursor:pointer;/"><b>" + i.toString() + "</b></span> "; } } if (this.totalPageCount > endPageNum) { var nextPageBeginNum = endPageNum + 1 < this.totalPageCount ? endPageNum + 1 : this.totalPageCount; str = str + " <span οnclick='pager.changePage(" + nextPageBeginNum + ")' style="/" mce_style="/""cursor:pointer;/"> >> </span> "; } document.getElementById(ele).innerHTML = ""; document.getElementById(ele).innerHTML = str; handler(); }; this.changePage = function(currentPage) { this.currentPage = currentPage; this.init(ele, handler); }; }