博客列表 >Ajax无刷新分页,当前页高亮显示

Ajax无刷新分页,当前页高亮显示

海阔天空
海阔天空原创
2020年05月30日 20:21:12825浏览

Ajax 无刷新分页,页码高亮显示

代码如下

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <script src="lib/jquery-3.5.1.js"></script>
  7. <title>Ajax无刷新分页</title>
  8. <style>
  9. table {
  10. border-collapse: collapse;
  11. border: 1px solid;
  12. text-align: center;
  13. margin: auto;
  14. width: 510px;
  15. box-shadow: 0px 0px 4px rgb(113, 114, 114);
  16. }
  17. table caption {
  18. font-size: 1.8rem;
  19. margin-bottom: 10px;
  20. text-shadow: 2px 2px 2px rgb(167, 168, 168);
  21. }
  22. th,
  23. td {
  24. border: 1px solid;
  25. padding: 5px;
  26. }
  27. tr:hover {
  28. background-color: #ccc;
  29. cursor: pointer;
  30. }
  31. thead tr:first-of-type {
  32. background-color: #ddd;
  33. }
  34. p {
  35. text-align: center;
  36. }
  37. p a {
  38. text-decoration: none;
  39. border: 1px solid;
  40. padding: 0 8px;
  41. }
  42. .active {
  43. background-color: rgb(206, 101, 101);
  44. border: 1px solid red;
  45. color: white;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <table>
  51. <caption>
  52. 员工信息表
  53. </caption>
  54. <thead>
  55. <tr>
  56. <th>id</th>
  57. <th>姓名</th>
  58. <th>年龄</th>
  59. <th>性别</th>
  60. <th>职位</th>
  61. <th>手机号</th>
  62. </tr>
  63. </thead>
  64. <tbody></tbody>
  65. </table>
  66. <!-- 分页条 -->
  67. <p></p>
  68. </body>
  69. <script>
  70. // 默认是第1页
  71. var page = 1;
  72. // 默认显示第一页,用一个函数来实现显示
  73. getPageData(page);
  74. // 分页函数
  75. function getPageData(page) {
  76. // ajax无刷新分页
  77. $.ajax({
  78. type: "post",
  79. url: "page_data.php",
  80. data: { page: page },
  81. dataType: "json",
  82. success: show,
  83. });
  84. }
  85. // 数据显示函数
  86. function show(data) {
  87. // 1. 显示表格数据
  88. var str = "";
  89. data.staffs.forEach(function (staff) {
  90. str += "<tr>";
  91. str += "<td>" + staff.id + "</td>";
  92. str += "<td><input type='text' value=" + staff.name + "></td>";
  93. str += "<td>" + staff.age + "</td>";
  94. str += "<td>" + staff.sex + "</td>";
  95. str += "<td>" + staff.position + "</td>";
  96. str += "<td>" + staff.mobile + "</td>";
  97. str += "</tr>";
  98. });
  99. $("tbody").html(str);
  100. // 2. 显示分页条
  101. var str = "";
  102. for (var i = 1; i <= data.pages; i++) {
  103. str += '<a href="" data-index=' + i + ">" + i + "</a> ";
  104. }
  105. // 将页码添到分页条, 并将当前页置为高亮
  106. $("p")
  107. .html(str)
  108. .find("a")
  109. .eq(page - 1)
  110. .addClass("active");
  111. // 分页条的点击事件
  112. $("p a").click(function (ev) {
  113. // 禁用<a>的跳转默认行为
  114. ev.preventDefault();
  115. page = $(this).attr("data-index");
  116. $("tbody").html("");
  117. getPageData(page);
  118. });
  119. }
  120. </script>
  121. </html>

效果图如下:

总结:
1、Ajax应用,按照老师归纳的步骤,实现起来比较优雅。
2、还需要多学、多记、多练,避免记混。
3、两个月的时间,大量的语法、丰富的函数,众多的案例,对全栈工作有了初步的认知。感谢朱老师的无私讲授、辛勤付出!

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议