首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用页面集进行jQuery分页

使用页面集进行jQuery分页
EN

Stack Overflow用户
提问于 2015-06-17 04:02:53
回答 1查看 473关注 0票数 0

我正在实现我的web应用程序的分页,并希望一次只显示5页(第1-5页,第6-10页等)。

假设我返回的总页数是100;使用for循环,我是否可以将我的5页集合从1-5更新为6-10,直到达到总页数?所以就像这样:

代码语言:javascript
运行
复制
 numOfElemnets = 5 //pages to be displayed at once
 totalPages = 100;
 for(var i=0; i<totalPages; i++) {
     $("#toolbar .pagination li a").attr('href', 'page=" + i + "'); //div containing <a> elements
 }

或者,如果我的概念完全错误,有更好的实现方法,请让我知道!

EN

回答 1

Stack Overflow用户

发布于 2015-06-17 04:15:47

它应该是这样的:

代码语言:javascript
运行
复制
 numOfElemnets = 5 //pages to be displayed at once
 totalPages = 100; //total pages
 currentPage = 3; //this is shouldn't be static, since it's the current page the user is at

 /**
 * If the user is on page 1 to 5, the first page will be 1. If he's not, then the page will be the current page minus 5.
 * This is to stop it from starting the loop from a negative number, since you don't have a -4 page.
 */
 if(currentPage > numOfElemnets) {
    i = currentPage - numOfElemnets;
 } else {
    i = 1;
 }

 // The same thing as above for the end. You will take 5 pages after the current page, but if you're on page 99, you should pick just till 100 that is your limit.
 if(currentPage + numOfElemnets > totalPages) {
    end = totalPages;
 } else {
    end = currentPage + numOfElemnets;
 }

  // I changed the for to a while, since the i parameter had to get out of the for. You can still use a for and give the i as a parameter. Whatever.
 while(i <= end) {
    $("#toolbar .pagination li a").attr('href', 'page=" + i + "'); //div containing <a> elements
    i++;
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30877089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档