gpt4 book ai didi

javascript - 带有页面集的 jQuery 分页

转载 作者:行者123 更新时间:2023-12-03 09:51:16 25 4
gpt4 key购买 nike

我正在为我的 Web 应用程序实现分页,并且希望一次仅显示 5 个页面(第 1-5 页、第 6-10 页等)

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

 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
}

enter image description here

或者,如果我的这个概念完全错误,并且有更好的实现方法,请告诉我!

最佳答案

它会是这样的:

 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++;
}

关于javascript - 带有页面集的 jQuery 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30877089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com