{"id":26707,"date":"2024-11-24T14:03:48","date_gmt":"2024-11-24T06:03:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26707\/"},"modified":"2024-11-24T14:03:48","modified_gmt":"2024-11-24T06:03:48","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-jquery-%e7%82%b9%e5%87%bb%e6%8c%89%e9%92%ae%e5%bc%b9%e7%aa%97%ef%bc%8c%e5%b9%b6%e9%80%9a%e8%bf%87-ajax-%e5%bc%82%e6%ad%a5%e5%8a%a0%e8%bd%bd%e4%b8%8d%e5%90%8c-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26707\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 jQuery \u70b9\u51fb\u6309\u94ae\u5f39\u7a97\uff0c\u5e76\u901a\u8fc7 AJAX \u5f02\u6b65\u52a0\u8f7d\u4e0d\u540c\u5206\u7c7b ID \u7684\u6570\u636e\uff0c\u5e76\u5728\u6bcf\u4e2a\u9009\u9879\u5361\u6eda\u52a8\u5230\u5e95\u90e8\u65f6\u8fdb\u884c\u7ffb\u9875\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173104940860077.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 jQuery \u70b9\u51fb\u6309\u94ae\u5f39\u7a97\uff0c\u5e76\u901a\u8fc7 AJAX \u5f02\u6b65\u52a0\u8f7d\u4e0d\u540c\u5206\u7c7b ID \u7684\u6570\u636e\uff0c\u5e76\u5728\u6bcf\u4e2a\u9009\u9879\u5361\u6eda\u52a8\u5230\u5e95\u90e8\u65f6\u8fdb\u884c\u7ffb\u9875\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 jQuery \u70b9\u51fb\u6309\u94ae\u5f39\u7a97\uff0c\u5e76\u901a\u8fc7 AJAX \u5f02\u6b65\u52a0\u8f7d\u4e0d\u540c\u5206\u7c7b ID \u7684\u6570\u636e\uff0c\u5e76\u5728\u6bcf\u4e2a\u9009\u9879\u5361\u6eda\u52a8\u5230\u5e95\u90e8\u65f6\u8fdb\u884c\u7ffb\u9875\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 jquery \u4e2d\u70b9\u51fb\u6309\u94ae\u5f39\u7a97\uff0c\u5e76\u4f7f\u7528 ajax \u4e0d\u540c\u5206\u7c7b id \u7684\u6570\u636e\uff0c\u5e76\u5728\u6bcf\u4e2a\u9009\u9879\u5361\u6eda\u52a8\u5230\u9875\u5e95\u540e\u8fdb\u884c\u7ffb\u9875\uff1f<\/strong><\/p>\n<p>\u5bf9\u4e8e\u8fd9\u4e2a\u95ee\u9898\uff0c\u89e3\u51b3\u65b9\u6848\u662f\uff1a<\/p>\n<ol>\n<li>\u521d\u59cb\u5316\u5206\u7c7b ID\u3001\u5f53\u524d\u9875\u7801\u548c\u603b\u9875\u6570\u3002<\/li>\n<li>\u76d1\u542c\u6309\u94ae\uff0c\u52a0\u8f7d\u7b2c\u4e00\u4e2a\u5206\u7c7b\u7684\u6570\u636e\u3002<\/li>\n<li>\u76d1\u542c\u9009\u9879\u5361\u5207\u6362\u4e8b\u4ef6\uff0c\u5207\u6362\u5206\u7c7b ID\uff0c\u91cd\u65b0\u52a0\u8f7d\u7b2c\u4e00\u9875\u6570\u636e\u3002<\/li>\n<li>\u76d1\u542c\u9009\u9879\u5361\u6eda\u52a8\u4e8b\u4ef6\uff0c\u5f53\u6eda\u52a8\u5230\u5e95\u90e8\u65f6\uff0c\u52a0\u8f7d\u4e0b\u4e00\u9875\u6570\u636e\u3002<\/li>\n<li>\u4f7f\u7528 AJAX \u8bf7\u6c42\u4ece\u540e\u7aef\u83b7\u53d6\u6570\u636e\uff0c\u66f4\u65b0\u9009\u9879\u5361\u5185\u5bb9\u3002<\/li>\n<\/ol>\n<p>\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/ \u521d\u59cb\u5316\u5206\u7c7b ID\u3001\u5f53\u524d\u9875\u7801\u548c\u603b\u9875\u6570\nlet categoryId = 1, currentPage = 1, total = 0;\n\/\/ \u662f\u5426\u52a0\u8f7d\u4e2d\nlet isLoading = false;\n\n$(document).on('click', '.btn', function() {\n  loadCategoryData(categoryId, currentPage);\n});\n\n$('.tab_p p').click(function() {\n  currentPage = 1;\n  categoryId = $(this).data('id');\n  \/\/ \u52a0\u8f7d\u5bf9\u5e94\u5206\u7c7b\u7684\u6570\u636e\n  loadCategoryData(categoryId, currentPage);\n});\n\nfunction loadCategoryData(id, page) {\n  $(\".tab_item\").html('\u52a0\u8f7d\u4e2d...');\n  $(this).addClass('cur').siblings().removeClass('cur');\n  loadPageData(id, page);\n}\n\n\/\/ \u76d1\u542c\u6eda\u52a8\u4e8b\u4ef6\n$('.tab_item').scroll(function() {\n  console.log('scroll...', $('.tab_item').scrollTop(), $('.tab_item').innerHeight())\n  if (isLoading) {\n    return;\n  }\n  \/\/ \u5224\u65ad\u662f\u5426\u6eda\u52a8\u5230\u5e95\u90e8\u8ddd\u79bb150px \u52a0\u8f7d\u66f4\u591a\n  const scrollTop = $(this).scrollTop();\n  const scrollHeight = $(this).prop('scrollHeight');\n  const containerHeight = $(this).outerHeight();\n\n  if (scrollHeight - scrollTop - containerHeight &lt; 150) {\n    \/\/ \u6eda\u52a8\u5230\u5e95\u90e8\u8ddd\u79bb\u5c0f\u4e8e150px\uff0c\u52a0\u8f7d\u66f4\u591a\u6570\u636e\n    currentPage++;\n    if (currentPage &lt;= total) {\n      loadPageData(categoryId, currentPage);\n    }\n  }\n});\n\nfunction loadPageData(categoryId, page) {\n  if (isLoading) {\n    return;\n  }\n  isLoading = true;\n  \n  $.ajax({\n    url: '\/ajax.php?mod=tab',\n    method: 'POST',\n    data: {\n      type_id: categoryId,\n      page: page\n    },\n    success: function(response) {\n      var obj = JSON.parse(response);\n      var html = \"\";\n      for (var i = 0; i &lt; obj.length; i++) {\n        html += '&lt;div&gt;@@##@@&lt;span&gt;\"' + obj[i].title + '\"&lt;\/span&gt;&lt;\/div&gt;';\n      }\n      $(\".tab_item\").append(html);\n    }\n  }).always(function() {\n    isLoading = false;\n  });\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u4f7f\u7528\u6a21\u62df\u6570\u636e\uff08getData\uff09\uff0c\u5b9e\u9645\u4e1a\u52a1\u4e2d\uff0cgetData \u9700\u8981\u66ff\u6362\u4e3a\u5b9e\u9645\u7684 AJAX \u8bf7\u6c42\u51fd\u6570\u3002<\/li>\n<li>\u5b9e\u9645\u4e1a\u52a1\u4e2d\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u6839\u636e\u9700\u6c42\u8c03\u6574\u6eda\u52a8\u8ddd\u79bb\u9608\u503c\uff08150px\uff09\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.php.cn\/faq\/'%20+%20obj%5Bi%5D.img_url%20+%20'\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 jQuery \u70b9\u51fb\u6309\u94ae\u5f39\u7a97\uff0c\u5e76\u901a\u8fc7 AJAX \u5f02\u6b65\u52a0\u8f7d\u4e0d\u540c\u5206\u7c7b ID \u7684\u6570\u636e\uff0c\u5e76\u5728\u6bcf\u4e2a\u9009\u9879\u5361\u6eda\u52a8\u5230\u5e95\u90e8\u65f6\u8fdb\u884c\u7ffb\u9875\uff1f\u63d2\u56fe1\" alt=\"\u5982\u4f55\u4f7f\u7528 jQuery \u70b9\u51fb\u6309\u94ae\u5f39\u7a97\uff0c\u5e76\u901a\u8fc7 AJAX \u5f02\u6b65\u52a0\u8f7d\u4e0d\u540c\u5206\u7c7b ID \u7684\u6570\u636e\uff0c\u5e76\u5728\u6bcf\u4e2a\u9009\u9879\u5361\u6eda\u52a8\u5230\u5e95\u90e8\u65f6\u8fdb\u884c\u7ffb\u9875\uff1f\u63d2\u56fe1\" \/> <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 jQuery \u70b9\u51fb\u6309\u94ae\u5f39\u7a97\uff0c\u5e76\u901a\u8fc7 AJAX \u4e0d\u540c\u5206\u7c7b ID \u7684\u6570\u636e\uff0c\u5e76\u5728\u6bcf\u4e2a\u9009\u9879\u5361\u6eda\u52a8\u5230\u5e95\u90e8\u65f6\u8fdb\u884c\u7ffb\u9875\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 jquery \u4e2d\u70b9\u51fb\u6309\u94ae\u5f39\u7a97\uff0c\u5e76\u4f7f\u7528 ajax \u4e0d\u540c\u5206\u7c7b id \u7684\u6570\u636e\uff0c\u5e76\u5728\u6bcf\u4e2a\u9009\u9879\u5361\u6eda\u52a8\u5230\u9875\u5e95\u540e\u8fdb\u884c\u7ffb\u9875\uff1f \u5bf9\u4e8e\u8fd9\u4e2a\u95ee\u9898\uff0c\u89e3\u51b3\u65b9\u6848\u662f\uff1a \u521d\u59cb\u5316\u5206\u7c7b ID\u3001\u5f53\u524d\u9875\u7801\u548c\u603b\u9875\u6570\u3002 \u76d1\u542c\u6309\u94ae\uff0c\u52a0\u8f7d\u7b2c\u4e00\u4e2a\u5206\u7c7b\u7684\u6570\u636e\u3002 \u76d1\u542c\u9009\u9879\u5361\u5207\u6362\u4e8b\u4ef6\uff0c\u5207\u6362\u5206\u7c7b ID\uff0c\u91cd\u65b0\u52a0\u8f7d\u7b2c\u4e00\u9875\u6570\u636e\u3002 \u76d1\u542c\u9009\u9879\u5361\u6eda\u52a8\u4e8b\u4ef6\uff0c\u5f53\u6eda\u52a8\u5230\u5e95\u90e8\u65f6\uff0c\u52a0\u8f7d\u4e0b\u4e00\u9875\u6570\u636e\u3002 \u4f7f\u7528 AJAX \u8bf7\u6c42\u4ece\u540e\u7aef\u83b7\u53d6\u6570\u636e\uff0c\u66f4\u65b0\u9009\u9879\u5361\u5185\u5bb9\u3002 \u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a \/\/ \u521d\u59cb\u5316\u5206\u7c7b ID\u3001\u5f53\u524d\u9875\u7801\u548c\u603b\u9875\u6570 let categoryId = 1, currentPage = 1, total = 0; \/\/ \u662f\u5426\u52a0\u8f7d\u4e2d let isLoading = false; $(document).on(&#8216;click&#8217;, &#8216;.btn&#8217;, function() { loadCategoryData(categoryId, currentPage); }); $(&#8216;.tab_p p&#8217;).click(function() { currentPage = 1; categoryId = $(this).data(&#8216;id&#8217;); \/\/ \u52a0\u8f7d\u5bf9\u5e94\u5206\u7c7b\u7684\u6570\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-26707","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=26707"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26707\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}