{"id":3665,"date":"2024-11-10T16:26:42","date_gmt":"2024-11-10T08:26:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3665\/"},"modified":"2024-11-10T16:26:42","modified_gmt":"2024-11-10T08:26:42","slug":"%e5%a6%82%e4%bd%95%e8%b7%a8%e5%9f%9f%e8%8e%b7%e5%8f%96-iframe-%e4%b8%ad%e7%bd%91%e9%a1%b5%e7%9a%84%e9%ab%98%e5%ba%a6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3665\/","title":{"rendered":"\u5982\u4f55\u8de8\u57df\u83b7\u53d6 iframe \u4e2d\u7f51\u9875\u7684\u9ad8\u5ea6\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173111457384456.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u8de8\u57df\u83b7\u53d6 iframe \u4e2d\u7f51\u9875\u7684\u9ad8\u5ea6\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u8de8\u57df\u83b7\u53d6 iframe \u4e2d\u7f51\u9875\u7684\u9ad8\u5ea6\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u8de8\u57df\u83b7\u53d6\u7f51\u9875\u9ad8\u5ea6<\/strong><\/p>\n<p>\u5728 web \u5f00\u53d1\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u83b7\u53d6 iframe \u4e2d\u52a0\u8f7d\u7684\u7f51\u9875\u7684\u9ad8\u5ea6\uff0c\u5373\u4f7f\u8be5\u7f51\u9875\u4f4d\u4e8e\u4e0d\u540c\u7684\u57df\u3002\u4f46\u7531\u4e8e\u8de8\u57df\u9650\u5236\uff0c\u76f4\u63a5\u4ece\u7236\u9875\u9762\u83b7\u53d6\u5b50\u9875\u9762\u9ad8\u5ea6\u53ef\u80fd\u65e0\u6cd5\u5b9e\u73b0\u3002<\/p>\n<p><strong>postmessage api<\/strong><\/p>\n<p>\u8981\u7ed5\u8fc7\u8de8\u57df\u9650\u5236\u5e76\u83b7\u53d6\u5b50\u9875\u9762\u7684\u9ad8\u5ea6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 postmessage api\u3002\u8be5 api \u5141\u8bb8\u60a8\u5728\u4e0d\u540c\u6765\u6e90\u7684\u7a97\u53e3\u4e4b\u95f4\u4f20\u9012\u6d88\u606f\uff0c\u4ece\u800c\u4f7f\u60a8\u53ef\u4ee5\u5728\u7236\u9875\u9762\u548c\u5b50\u9875\u9762\u4e4b\u95f4\u4ea4\u6362\u4fe1\u606f\u3002<\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5728\u5b50\u9875\u9762\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u4ee5\u54cd\u5e94\u7236\u9875\u9762\u7684\u6d88\u606f\u8bf7\u6c42\uff1a<\/p>\n<pre>window.addeventlistener(\"message\", (event) =&gt; {\n  if (event.data === \"getheight\") {\n    event.source.postmessage(document.body.scrollheight, \"*\");\n  }\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u5728\u7236\u9875\u9762\u4e2d\uff0c\u60a8\u53ef\u4ee5\u53d1\u9001\u6d88\u606f\u5e76\u76d1\u542c\u5b50\u9875\u9762\u7684\u54cd\u5e94\uff1a<\/p>\n<pre>const iframe = document.getElementById(\"c_iframe\");\n\niframe.onload = () =&gt; {\n  iframe.contentWindow.postMessage(\"getHeight\", \"*\");\n\n  window.addEventListener(\"message\", (event) =&gt; {\n    if (event.data &amp;&amp; iframe.contentWindow === event.source) {\n      iframe.height = event.data;\n    }\n  });\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u8be5\u65b9\u6cd5\u9700\u8981\u60a8\u80fd\u591f\u5728\u5b50\u9875\u9762\u4e2d\u6dfb\u52a0\u811a\u672c\u4ee3\u7801\u3002\u5982\u679c\u65e0\u6cd5\u5e72\u9884\u5b50\u9875\u9762\uff0c\u5219\u65e0\u6cd5\u901a\u8fc7\u6b64\u65b9\u5f0f\u83b7\u53d6\u5b50\u9875\u9762\u9ad8\u5ea6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u8de8\u57df\u83b7\u53d6 iframe \u4e2d\u7f51\u9875\u7684\u9ad8\u5ea6\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>\u8de8\u57df\u83b7\u53d6\u7f51\u9875\u9ad8\u5ea6 \u5728 web \u5f00\u53d1\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u83b7\u53d6 iframe \u4e2d\u52a0\u8f7d\u7684\u7f51\u9875\u7684\u9ad8\u5ea6\uff0c\u5373\u4f7f\u8be5\u7f51\u9875\u4f4d\u4e8e\u4e0d\u540c\u7684\u57df\u3002\u4f46\u7531\u4e8e\u8de8\u57df\u9650\u5236\uff0c\u76f4\u63a5\u4ece\u7236\u9875\u9762\u83b7\u53d6\u5b50\u9875\u9762\u9ad8\u5ea6\u53ef\u80fd\u65e0\u6cd5\u5b9e\u73b0\u3002 postmessage api \u8981\u7ed5\u8fc7\u8de8\u57df\u9650\u5236\u5e76\u83b7\u53d6\u5b50\u9875\u9762\u7684\u9ad8\u5ea6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 postmessage api\u3002\u8be5 api \u5141\u8bb8\u60a8\u5728\u4e0d\u540c\u6765\u6e90\u7684\u7a97\u53e3\u4e4b\u95f4\u4f20\u9012\u6d88\u606f\uff0c\u4ece\u800c\u4f7f\u60a8\u53ef\u4ee5\u5728\u7236\u9875\u9762\u548c\u5b50\u9875\u9762\u4e4b\u95f4\u4ea4\u6362\u4fe1\u606f\u3002 \u9996\u5148\uff0c\u9700\u8981\u5728\u5b50\u9875\u9762\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u4ee5\u54cd\u5e94\u7236\u9875\u9762\u7684\u6d88\u606f\u8bf7\u6c42\uff1a window.addeventlistener(&#8220;message&#8221;, (event) =&gt; { if (event.data === &#8220;getheight&#8221;) { event.source.postmessage(document.body.scrollheight, &#8220;*&#8221;); } }); \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u5728\u7236\u9875\u9762\u4e2d\uff0c\u60a8\u53ef\u4ee5\u53d1\u9001\u6d88\u606f\u5e76\u76d1\u542c\u5b50\u9875\u9762\u7684\u54cd\u5e94\uff1a const iframe = document.getElementById(&#8220;c_iframe&#8221;); iframe.onload = () =&gt; { iframe.contentWindow.postMessage(&#8220;getHeight&#8221;, &#8220;*&#8221;); window.addEventListener(&#8220;message&#8221;, (event) =&gt; { if (event.data &amp;&amp; iframe.contentWindow === event.source) { iframe.height = event.data; } [&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-3665","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3665","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=3665"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3665\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}