{"id":1715,"date":"2024-11-10T17:49:26","date_gmt":"2024-11-10T09:49:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1715\/"},"modified":"2024-11-10T17:49:26","modified_gmt":"2024-11-10T09:49:26","slug":"%e9%a1%b5%e9%9d%a2%e5%88%b7%e6%96%b0%e6%97%b6onload%e4%ba%8b%e4%bb%b6%e5%a6%82%e4%bd%95%e6%89%a7%e8%a1%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1715\/","title":{"rendered":"\u9875\u9762\u5237\u65b0\u65f6onload\u4e8b\u4ef6\u5982\u4f55\u6267\u884c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173054165894845.jpg\" class=\"aligncenter\" title=\"\u9875\u9762\u5237\u65b0\u65f6onload\u4e8b\u4ef6\u5982\u4f55\u6267\u884c\uff1f\u63d2\u56fe\" alt=\"\u9875\u9762\u5237\u65b0\u65f6onload\u4e8b\u4ef6\u5982\u4f55\u6267\u884c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u9875\u9762\u5237\u65b0\u65f6onload\u7684\u6267\u884c\u673a\u5236<\/strong><\/p>\n<p>\u5f53\u5237\u65b0\u9875\u9762\u65f6\uff0c\u4f1a\u91cd\u65b0\u52a0\u8f7d\u6574\u4e2a\u7f51\u9875\u7684\u5185\u5bb9\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0conload\u4e8b\u4ef6\u4f1a\u518d\u6b21\u89e6\u53d1\uff0c\u56e0\u4e3a\u5b83\u8d1f\u8d23\u5728\u52a0\u8f7d\u548c\u6e32\u67d3\u9875\u9762\u65f6\u6267\u884c\u7279\u5b9a\u7684\u52a8\u4f5c\u3002<\/p>\n<p><strong>DOM\u5143\u7d20\u4e2d\u7684onload\u4e8b\u4ef6<\/strong><\/p>\n<p>\u95ee\u9898\u4e2d\u63d0\u5230\u7684onload\u4e8b\u4ef6\u901a\u5e38\u7528\u4e8e\u6574\u4e2a\u9875\u9762\uff0c\u800c\u4e0d\u662f\u50cfdiv\u8fd9\u6837\u7279\u5b9a\u7684DOM\u5143\u7d20\u3002div\u5143\u7d20\u6ca1\u6709onload\u4e8b\u4ef6\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u4e8b\u4ef6\u6765\u54cd\u5e94div\u7684\u52a0\u8f7d\u548c\u6e32\u67d3\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u7f51\u9875\u4e2d\u5e38\u5e38\u4f7f\u7528\u4ee5\u4e0b\u7684load\u4e8b\u4ef6\u76d1\u542c\u6574\u4e2abody\u6807\u7b7e\u52a0\u8f7d\u5e76\u6e32\u67d3\u5b8c\u6210\uff1a<\/p>\n<pre>&lt;body onload=\"init()\"&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u4e8b\u4ef6\u53ef\u5728\u6574\u4e2a\u9875\u9762\u53ca\u6240\u6709\u4f9d\u8d56\u8d44\u6e90\uff08\u4f8b\u5982\u6837\u5f0f\u8868\u548c\u56fe\u50cf\uff09\u52a0\u8f7d\u5b8c\u6bd5\u540e\u89e6\u53d1\u3002\u4f46\u662f\uff0c\u5b83\u4e0ediv\u5143\u7d20\u4e2d\u7684onload\u4e8b\u4ef6\u65e0\u5173\u3002<\/p>\n<p>\u4e3a\u4e86\u76d1\u542c\u5355\u4e2aDOM\u5143\u7d20\u7684\u52a0\u8f7d\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982DOMContentLoaded\u7684API\u3002\u5b83\u4f1a\u5728\u6240\u6709DOM\u5143\u7d20\u548c\u4f9d\u8d56\u8d44\u6e90\u52a0\u8f7d\u5e76\u89e3\u6790\u540e\u89e6\u53d1\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u76d1\u6d4b\u4e00\u4e2a\u7279\u5b9aDOM\u5143\u7d20\u7684\u6e32\u67d3\u5b8c\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li> <strong>MutationObserver\uff1a<\/strong>\u5b83\u53ef\u4ee5\u76d1\u89c6DOM\u6811\u7684\u66f4\u6539\uff0c\u5305\u62ec\u5143\u7d20\u7684\u6dfb\u52a0\u3001\u5220\u9664\u548c\u4fee\u6539\u3002<\/li>\n<li> <strong>\u6846\u67b6\uff1a<\/strong>\u50cfReact\u548cVue\u8fd9\u6837\u7684\u6846\u67b6\u63d0\u4f9b\u4e86\u751f\u547d\u5468\u671f\u94a9\u5b50\uff0c\u53ef\u4ee5\u7528\u6765\u76d1\u89c6\u7ec4\u4ef6\u7684\u7279\u5b9a\u52a0\u8f7d\u548c\u6e32\u67d3\u9636\u6bb5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u9875\u9762\u5237\u65b0\u65f6onload\u4e8b\u4ef6\u5982\u4f55\u6267\u884c\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>\u9875\u9762\u5237\u65b0\u65f6onload\u7684\u6267\u884c\u673a\u5236 \u5f53\u5237\u65b0\u9875\u9762\u65f6\uff0c\u4f1a\u91cd\u65b0\u52a0\u8f7d\u6574\u4e2a\u7f51\u9875\u7684\u5185\u5bb9\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0conload\u4e8b\u4ef6\u4f1a\u518d\u6b21\u89e6\u53d1\uff0c\u56e0\u4e3a\u5b83\u8d1f\u8d23\u5728\u52a0\u8f7d\u548c\u6e32\u67d3\u9875\u9762\u65f6\u6267\u884c\u7279\u5b9a\u7684\u52a8\u4f5c\u3002 DOM\u5143\u7d20\u4e2d\u7684onload\u4e8b\u4ef6 \u95ee\u9898\u4e2d\u63d0\u5230\u7684onload\u4e8b\u4ef6\u901a\u5e38\u7528\u4e8e\u6574\u4e2a\u9875\u9762\uff0c\u800c\u4e0d\u662f\u50cfdiv\u8fd9\u6837\u7279\u5b9a\u7684DOM\u5143\u7d20\u3002div\u5143\u7d20\u6ca1\u6709onload\u4e8b\u4ef6\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u4e8b\u4ef6\u6765\u54cd\u5e94div\u7684\u52a0\u8f7d\u548c\u6e32\u67d3\u3002 \u6bd4\u5982\uff0c\u7f51\u9875\u4e2d\u5e38\u5e38\u4f7f\u7528\u4ee5\u4e0b\u7684load\u4e8b\u4ef6\u76d1\u542c\u6574\u4e2abody\u6807\u7b7e\u52a0\u8f7d\u5e76\u6e32\u67d3\u5b8c\u6210\uff1a &lt;body onload=&#8221;init()&#8221;&gt; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u4e8b\u4ef6\u53ef\u5728\u6574\u4e2a\u9875\u9762\u53ca\u6240\u6709\u4f9d\u8d56\u8d44\u6e90\uff08\u4f8b\u5982\u6837\u5f0f\u8868\u548c\u56fe\u50cf\uff09\u52a0\u8f7d\u5b8c\u6bd5\u540e\u89e6\u53d1\u3002\u4f46\u662f\uff0c\u5b83\u4e0ediv\u5143\u7d20\u4e2d\u7684onload\u4e8b\u4ef6\u65e0\u5173\u3002 \u4e3a\u4e86\u76d1\u542c\u5355\u4e2aDOM\u5143\u7d20\u7684\u52a0\u8f7d\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982DOMContentLoaded\u7684API\u3002\u5b83\u4f1a\u5728\u6240\u6709DOM\u5143\u7d20\u548c\u4f9d\u8d56\u8d44\u6e90\u52a0\u8f7d\u5e76\u89e3\u6790\u540e\u89e6\u53d1\u3002 \u5982\u679c\u60a8\u9700\u8981\u76d1\u6d4b\u4e00\u4e2a\u7279\u5b9aDOM\u5143\u7d20\u7684\u6e32\u67d3\u5b8c\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a MutationObserver\uff1a\u5b83\u53ef\u4ee5\u76d1\u89c6DOM\u6811\u7684\u66f4\u6539\uff0c\u5305\u62ec\u5143\u7d20\u7684\u6dfb\u52a0\u3001\u5220\u9664\u548c\u4fee\u6539\u3002 \u6846\u67b6\uff1a\u50cfReact\u548cVue\u8fd9\u6837\u7684\u6846\u67b6\u63d0\u4f9b\u4e86\u751f\u547d\u5468\u671f\u94a9\u5b50\uff0c\u53ef\u4ee5\u7528\u6765\u76d1\u89c6\u7ec4\u4ef6\u7684\u7279\u5b9a\u52a0\u8f7d\u548c\u6e32\u67d3\u9636\u6bb5\u3002 \u4ee5\u4e0a\u5c31\u662f\u9875\u9762\u5237\u65b0\u65f6onload\u4e8b\u4ef6\u5982\u4f55\u6267\u884c\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},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1715","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1715","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=1715"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1715\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}