我正在尝试使用 jQuery 模拟分页 并已经制定了所有必要的分页元素,但无法使导航部分正常工作。它适用于列表元素,但不适用于此表结构。有人可以告诉我可能是什么问题吗?
var show_per_page = 2;
var number_of_items = $('tbody').children().size();
var number_of_pages = Math.ceil(number_of_items / show_per_page);
var current_link = 0;
$('table').after('<div class=controls></div>');
var navigation_html = '<a class="prev" onclick="previous()">...</a>';
while (number_of_pages > current_link) {
navigation_html += '<a class="page" onclick="go_to_page(' + current_link + ')" longdesc="' + current_link + '">' + (current_link + 1) + '</a>';
current_link++;
}
navigation_html += '<a class="next" onclick="next()">...</a>';
$('.controls').html(navigation_html);
$('.controls .page:first').addClass('active');
$('tbody').children().hide();
$('tbody').children().slice(0, show_per_page).show();
function go_to_page(page_num) {
start_from = page_num * show_per_page;
end_on = start_from + show_per_page;
$('tbody').children().hide().slice(start_from, end_on).show();
$('.page[longdesc=' + page_num + ']').addClass('active').siblings('.active').removeClass('active');
}
function previous() {
new_page = current_link - 1;
if ($('.active').prev('.page').length == true) {
go_to_page(new_page);
}
}
function next() {
new_page = current_link + 1;
if ($('.active').next('.page').length == true) {
go_to_page(new_page);
}
$("a.prev").show();
}
演示 JS Fiddle
最佳答案
你的代码可以工作,它与 JSfiddle 包装函数名称有关
我在本地文件上运行了您的代码,只需要包含 jquery。
这是我使用的整个代码:https://gist.github.com/kEpEx/4422d441dbd0b71dfd3d
关于javascript - 使用 jQuery 进行表格分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35166368/