亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 52
粉絲 0
評論 3
訪問量 53037
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
js學(xué)習(xí):第21章 事件與Ajax實(shí)戰(zhàn)、無刷分頁(補(bǔ))
王小飛
原創(chuàng)
1443人瀏覽過

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()請求數(shù)據(jù)</button>
  25. <button type="button">2. $.get()請求數(shù)據(jù)</button>
  26. <button type="button">3. $.post()請求數(shù)據(jù)</button>
  27. <button type="button">4. $.getJSON()請求JSON數(shù)據(jù)</button>
  28. <button type="button">5. $.ajax()請求數(shù)據(jù)</button>
  29. <button type="button">6. $.ajax()-jsonp-跨域請求數(shù)據(jù)1</button>
  30. <button type="button">7. $.ajax()-jsonp-跨域請求數(shù)據(jù)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方式從服務(wù)器獲取資源/數(shù)據(jù)
  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方式從服務(wù)器獲取資源/數(shù)據(jù)
  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對象了, 不必轉(zhuǎn)換
  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(): 終級方法, 實(shí)際上大家只需要掌握這一個(gè)方法
  67. // $.ajax({
  68. // // 請求類型
  69. // type: "GET",
  70. // // 請求的URL
  71. // url: url,
  72. // // 發(fā)送的數(shù)據(jù)
  73. // data: data,
  74. // // 期望服務(wù)器返回/響應(yīng)的數(shù)據(jù)類型
  75. // dataType: "json",
  76. // // 成功時(shí)的回調(diào)函數(shù)
  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無刷新分頁技術(shù)</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. 消費(fèi)明細(xì)表表
  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. // 默認(rèn)是第1頁
  63. var page = 1;
  64. // 默認(rèn)顯示第一頁,用一個(gè)函數(shù)來實(shí)現(xiàn)顯示
  65. getPageData(page);
  66. // 分頁函數(shù)
  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. // 數(shù)據(jù)顯示函數(shù)
  78. function show(data) {
  79. // 1. 顯示表格數(shù)據(jù)
  80. console.log(data);
  81. console.log(data.pages);
  82. console.log(data.staffs);
  83. var str = "";
  84. data.staffs.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. // 將頁碼添到分頁條, 并將當(dāng)前頁置為高亮
  103. $("p")
  104. .html(str)
  105. .find("a")
  106. .eq(page - 1)
  107. .addClass("active");
  108. // 分頁條的點(diǎn)擊事件
  109. $("p a").click(function (ev) {
  110. // 禁用<a>的跳轉(zhuǎn)默認(rèn)行為
  111. ev.preventDefault();
  112. page = $(this).attr("data-index");
  113. $("tbody").html("");
  114. getPageData(page);
  115. });
  116. }
  117. </script>
  118. </html>

效果:

批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語:設(shè)置頁碼高亮是不是超級簡單, 就一個(gè)窗戶紙
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)