当前位置:首页 > jquery

jquery 翻页

2026-04-08 04:33:24jquery

jQuery 翻页实现方法

基础翻页结构

使用简单的HTML和jQuery实现翻页功能,需包含页码按钮和内容容器:

<div class="pagination">
  <button class="prev">上一页</button>
  <span class="page-numbers"></span>
  <button class="next">下一页</button>
</div>
<div class="content-container"></div>

动态生成页码

通过jQuery动态计算并渲染页码按钮,需定义总页数和当前页:

jquery 翻页

let currentPage = 1;
const totalPages = 10;

function renderPagination() {
  $('.page-numbers').empty();
  for (let i = 1; i <= totalPages; i++) {
    $('.page-numbers').append(`<button class="page-btn ${i === currentPage ? 'active' : ''}">${i}</button>`);
  }
}

翻页事件绑定

为页码按钮和导航按钮添加点击事件处理:

$(document).on('click', '.page-btn', function() {
  currentPage = parseInt($(this).text());
  loadPageContent();
});

$('.prev').click(function() {
  if (currentPage > 1) {
    currentPage--;
    loadPageContent();
  }
});

$('.next').click(function() {
  if (currentPage < totalPages) {
    currentPage++;
    loadPageContent();
  }
});

AJAX加载内容

结合AJAX实现动态内容加载(需后端接口支持):

jquery 翻页

function loadPageContent() {
  $.ajax({
    url: '/api/data',
    data: { page: currentPage },
    success: function(response) {
      $('.content-container').html(response.data);
      renderPagination();
    }
  });
}

样式优化建议

为当前页码添加高亮样式,增强用户体验:

.page-numbers button.active {
  background-color: #007bff;
  color: white;
}
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  cursor: pointer;
}

完整示例代码

整合所有功能的完整实现示例:

<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <style>
    .pagination { margin: 20px 0; }
    .page-numbers button { margin: 0 5px; }
    .active { font-weight: bold; color: red; }
  </style>
</head>
<body>
  <div class="pagination">
    <button class="prev">上一页</button>
    <span class="page-numbers"></span>
    <button class="next">下一页</button>
  </div>
  <div class="content-container"></div>

  <script>
    $(function() {
      let currentPage = 1;
      const totalPages = 5;

      function renderPagination() {
        $('.page-numbers').empty();
        for (let i = 1; i <= totalPages; i++) {
          $('.page-numbers').append(
            `<button class="page-btn ${i === currentPage ? 'active' : ''}">${i}</button>`
          );
        }
      }

      function loadPageContent() {
        $('.content-container').html(`当前显示第 ${currentPage} 页内容`);
        renderPagination();
      }

      $(document).on('click', '.page-btn', function() {
        currentPage = parseInt($(this).text());
        loadPageContent();
      });

      $('.prev').click(function() {
        if (currentPage > 1) {
          currentPage--;
          loadPageContent();
        }
      });

      $('.next').click(function() {
        if (currentPage < totalPages) {
          currentPage++;
          loadPageContent();
        }
      });

      // 初始化
      loadPageContent();
    });
  </script>
</body>
</html>

注意事项

  • 总页数通常由后端接口返回,示例中使用固定值简化实现
  • 实际项目中建议添加加载状态提示和错误处理
  • 移动端适配可考虑使用响应式布局或滑动翻页组件
  • 大数据量时建议实现分页缓存机制

标签: 翻页jquery
分享给朋友:

相关文章

vue实现翻页

vue实现翻页

Vue实现翻页功能 翻页功能通常结合后端API分页数据实现,以下是基于Vue 2或Vue 3的实现方案: 基础数据准备 需要定义当前页码、每页条数、总数据量等核心变量: data() { re…

jquery的

jquery的

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它的核心理念是“写得更少,做得更多”(Write Le…

jquery css

jquery css

jQuery 操作 CSS 的方法 jQuery 提供了多种方法来操作元素的 CSS 样式,包括获取、设置、添加或移除样式。以下是常用的方法: 获取 CSS 属性值 使用 .css() 方法获取元素…

jquery 教程

jquery 教程

jQuery 教程概览 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。以下是学习 jQuery 的核心内容和方法。 基础语…

vue实现滑动翻页

vue实现滑动翻页

Vue 实现滑动翻页的方法 使用 touch 事件监听 在 Vue 组件中监听 touchstart、touchmove 和 touchend 事件,通过计算滑动距离判断翻页方向。 <temp…

jquery img

jquery img

jQuery 操作图片(img)元素的方法 jQuery 提供了多种方法来操作和修改 HTML 中的 <img> 元素。以下是常见的几种用法: 修改图片的 src 属性 使用 .attr…