Blogger Information
Blog 52
fans 0
comment 3
visits 42307
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js学习:第21章 事件与Ajax实战、无刷分页
王小飞
Original
681 people have browsed it

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. body {
  10. display: grid;
  11. gap: 15px;
  12. }
  13. button {
  14. text-align: left;
  15. height: 26px;
  16. }
  17. button:hover {
  18. background-color: #ddd;
  19. cursor: pointer;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <button type="button">1. load()请求数据</button>
  25. <button type="button">2. $.get()请求数据</button>
  26. <button type="button">3. $.post()请求数据</button>
  27. <button type="button">4. $.getJSON()请求JSON数据</button>
  28. <button type="button">5. $.ajax()请求数据</button>
  29. <button type="button">6. $.ajax()-jsonp-跨域请求数据1</button>
  30. <button type="button">7. $.ajax()-jsonp-跨域请求数据2</button>
  31. </body>
  32. </html>
  33. <script>
  34. var cl = console.log.bind(console);
  35. // 1. load(): 加载html片断
  36. $("button:first-of-type").click(function () {
  37. $(this).after("<div>").next().load("nav.html");
  38. });
  39. // 2. $.get():以get方式从服务器获取资源/数据
  40. $("button:nth-of-type(2)").click(function (ev) {
  41. // $.get(url, data, callback)
  42. $.get("users.php", { id: 2 }, function (data) {
  43. // cl(data);
  44. $(ev.target).after("<div>").next().html(data);
  45. });
  46. });
  47. // 3. $.post():以post方式从服务器获取资源/数据
  48. $("button:nth-of-type(3)").click(function (ev) {
  49. // $.post(url, data, callback)
  50. $.post("users.php", { id: 4 }, function (data) {
  51. // cl(data);
  52. $(ev.target).after("<div>").next().html(data);
  53. });
  54. });
  55. // 4. $.getJSON():接口返回的大多是JSON
  56. $("button:nth-of-type(4)").click(function (ev) {
  57. // $.getJSON(url, data, callback)
  58. $.getJSON("users.php?id=2", function (data) {
  59. // 返回就是js对象了, 不必转换
  60. // cl(JSON.parse(data));
  61. cl(data);
  62. var res = data.id + ": " + data.name + ", " + data.age + "岁";
  63. $(ev.target).after("<div>").next().html(res);
  64. });
  65. });
  66. // 5. $.ajax(): 终级方法, 实际上大家只需要掌握这一个方法
  67. // $.ajax({
  68. // // 请求类型
  69. // type: "GET",
  70. // // 请求的URL
  71. // url: url,
  72. // // 发送的数据
  73. // data: data,
  74. // // 期望服务器返回/响应的数据类型
  75. // dataType: "json",
  76. // // 成功时的回调函数
  77. // success: callback,
  78. // });
  79. $("button:nth-of-type(5)").click(function (ev) {
  80. $.ajax({
  81. type: "GET",
  82. url: "users.php",
  83. data: { id: 10 },
  84. dataType: "html",
  85. success: function (data) {
  86. $(ev.target).after("<div>").next().html(data);
  87. },
  88. });
  89. });
  90. // "http://php.edu/0522/test2.php?jsonp=handle&id=3";
  91. // 6. $.ajax()-jsonp-1:跨域请求
  92. $("button:nth-of-type(6)").click(function (ev) {
  93. $.ajax({
  94. type: "GET",
  95. url: "http://127.0.0.1/0522/test2.php?jsonp=?&id=2",
  96. dataType: "jsonp",
  97. success: function (data) {
  98. cl(data);
  99. var data = JSON.parse(data);
  100. cl(data);
  101. var data =
  102. "<p>" +
  103. data.title +
  104. "</p><p>" +
  105. data.user.name +
  106. ", 邮箱:" +
  107. data.user.email +
  108. "</p>";
  109. $(ev.target).after("<div>").next().html(data);
  110. },
  111. });
  112. });
  113. // 7. $.ajax()-jsonp-2:跨域请求
  114. $("button:last-of-type").click(function (ev) {
  115. $.ajax({
  116. type: "GET",
  117. url: "http://127.0.0.1/0522/test2.php?jsonp=?&id=2",
  118. dataType: "jsonp",
  119. jsonpCallback: "handle",
  120. });
  121. });
  122. function handle(data) {
  123. cl(data);
  124. var data = JSON.parse(data);
  125. cl(data);
  126. var data =
  127. "<p>" +
  128. data.title +
  129. "</p><p>" +
  130. data.user.name +
  131. ", 邮箱:" +
  132. data.user.email +
  133. "</p>";
  134. $("button:last-of-type").after("<div>").next().html(data);
  135. }
  136. </script>

效果:

2. 无刷分页

代码:

  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: 500px;
  15. }
  16. table caption {
  17. font-size: 1.2rem;
  18. margin-bottom: 10px;
  19. }
  20. th,
  21. td {
  22. border: 1px solid;
  23. padding: 5px;
  24. }
  25. thead tr:first-of-type {
  26. background-color: #ddd;
  27. }
  28. p {
  29. text-align: center;
  30. }
  31. p a {
  32. text-decoration: none;
  33. border: 1px solid;
  34. padding: 0 8px;
  35. }
  36. .active {
  37. background-color: #ddd;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <table>
  43. <caption>
  44. 消费明细表表
  45. </caption>
  46. <thead>
  47. <tr>
  48. <th>id</th>
  49. <th>金额</th>
  50. <th>账户</th>
  51. <th>成员</th>
  52. <th>备注</th>
  53. <th>用户id</th>
  54. </tr>
  55. </thead>
  56. <tbody></tbody>
  57. </table>
  58. <!-- 分页条 -->
  59. <p></p>
  60. </body>
  61. <script>
  62. // 默认是第1页
  63. var page = 1;
  64. // 默认显示第一页,用一个函数来实现显示
  65. getPageData(page);
  66. // 分页函数
  67. function getPageData(page) {
  68. // ajax无刷新分页
  69. $.ajax({
  70. type: "post",
  71. url: "page_data.php",
  72. data: { page: page },
  73. dataType: "json",
  74. success: show,
  75. });
  76. }
  77. // 数据显示函数
  78. function show(data) {
  79. // 1. 显示表格数据
  80. console.log(data);
  81. console.log(data.pages);
  82. console.log(data.jizhang);
  83. var str = "";
  84. data.jizhang.forEach(function (staff) {
  85. str += "<tr>";
  86. str += "<td>" + staff.id + "</td>";
  87. str += "<td>" + staff.jine + "</td>";
  88. str += "<td>" + staff.zhanghu + "</td>";
  89. str += "<td>" + staff.chengyuan + "</td>";
  90. str += "<td>" + staff.beizhu + "</td>";
  91. str += "<td>" + staff.yonghuid + "</td>";
  92. str += "</tr>";
  93. });
  94. // $("tbody").append(str);
  95. $("tbody").html(str);
  96. // 2. 显示分页条
  97. var str = "";
  98. for (var i = 1; i <= data.pages; i++) {
  99. // $("<a>").attr("href", "").attr("data-index", i).html(i).appendTo("p");
  100. str += '<a href="" data-index=' + i + ">" + i + "</a>";
  101. }
  102. // 将页码添到分页条, 并将第一个置为高亮
  103. $("p").html(str).find("a").first().addClass("active");
  104. // 分页条的点击事件
  105. $("p a").click(function (ev) {
  106. // 禁用<a>的跳转默认行为
  107. ev.preventDefault();
  108. var page = $(this).attr("data-index");
  109. // $("a").$(this).attr("data-index").addClass("active");
  110. $("tbody").html("");
  111. getPageData(page);
  112. });
  113. }
  114. </script>
  115. </html>

效果:

Correcting teacher:天蓬老师天蓬老师

Correction status:unqualified

Teacher's comments:页码高亮未完成
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post