jq写带省略号的分页器
时间: 2023-12-08 17:59:53 浏览: 72
```
# 定义当前页和总页数
$current_page = 1;
$total_pages = 10;
# 定义分页器最多显示的页码数
$max_pages = 5;
# 计算分页器起始页码和结束页码
$start_page = $current_page - floor($max_pages / 2);
if ($start_page < 1) {
$start_page = 1;
}
$end_page = $start_page + $max_pages - 1;
if ($end_page > $total_pages) {
$end_page = $total_pages;
$start_page = $end_page - $max_pages + 1;
if ($start_page < 1) {
$start_page = 1;
}
}
# 输出分页器
echo '<ul class="pagination">';
if ($current_page > 1) {
echo '<li><a href="?page=' . ($current_page - 1) . '">上一页</a></li>';
}
if ($start_page > 1) {
echo '<li><a href="?page=1">1</a></li>';
if ($start_page > 2) {
echo '<li>...</li>';
}
}
for ($i = $start_page; $i <= $end_page; $i++) {
if ($i == $current_page) {
echo '<li class="active"><a href="#">' . $i . '</a></li>';
} else {
echo '<li><a href="?page=' . $i . '">' . $i . '</a></li>';
}
}
if ($end_page < $total_pages) {
if ($end_page < $total_pages - 1) {
echo '<li>...</li>';
}
echo '<li><a href="?page=' . $total_pages . '">' . $total_pages . '</a></li>';
}
if ($current_page < $total_pages) {
echo '<li><a href="?page=' . ($current_page + 1) . '">下一页</a></li>';
}
echo '</ul>';
```
上面的代码是php代码,如果需要转换为jq代码,可以按照以下步骤进行:
1. 将php变量改为jq变量,例如将"$current_page"改为"current_page";
2. 将php的"echo"改为jq的"$().append()";
3. 将php的"."改为jq的"+";
4. 将php的";"改为jq的";"或者省略掉。
转换后的代码如下所示:
```
# 定义当前页和总页数
var current_page = 1;
var total_pages = 10;
# 定义分页器最多显示的页码数
var max_pages = 5;
# 计算分页器起始页码和结束页码
var start_page = current_page - Math.floor(max_pages / 2);
if (start_page < 1) {
start_page = 1;
}
var end_page = start_page + max_pages - 1;
if (end_page > total_pages) {
end_page = total_pages;
start_page = end_page - max_pages + 1;
if (start_page < 1) {
start_page = 1;
}
}
# 输出分页器
$('ul.pagination').empty();
if (current_page > 1) {
$('ul.pagination').append('<li><a href="?page=' + (current_page - 1) + '">上一页</a></li>');
}
if (start_page > 1) {
$('ul.pagination').append('<li><a href="?page=1">1</a></li>');
if (start_page > 2) {
$('ul.pagination').append('<li>...</li>');
}
}
for (var i = start_page; i <= end_page; i++) {
if (i == current_page) {
$('ul.pagination').append('<li class="active"><a href="#">' + i + '</a></li>');
} else {
$('ul.pagination').append('<li><a href="?page=' + i + '">' + i + '</a></li>');
}
}
if (end_page < total_pages) {
if (end_page < total_pages - 1) {
$('ul.pagination').append('<li>...</li>');
}
$('ul.pagination').append('<li><a href="?page=' + total_pages + '">' + total_pages + '</a></li>');
}
if (current_page < total_pages) {
$('ul.pagination').append('<li><a href="?page=' + (current_page + 1) + '">下一页</a></li>');
}
```
阅读全文