iframe \u5411\u7236\u7ea7\u9875\u9762\u53d1\u9001\u6d88\u606f<\/strong><\/div>\n\u5728 iframe \u9875\u9762\uff0c\u4f7f\u7528 window.parent \u8c03\u7528 postMessage \u65b9\u6cd5\uff0c\u5373\u53ef\u53d1\u9001\u6d88\u606f\u7ed9\u7236\u7ea7\u9875\u9762\u3002<\/p>\n
window.parent.postMessage(message, '*');<\/pre>\nmessage \u53ea\u80fd\u662f String \u7c7b\u578b\uff0c\u6240\u4ee5\u5982\u679c\u60f3\u53d1\u9001\u591a\u6761\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 JSON \u5e8f\u5217\u5316\u5bf9\u8c61\u3002<\/p>\n
\/\/ \u5e8f\u5217\u5316\u5bf9\u8c61\r\nconst message = JSON.stringify({\r\n message: 'Hello',\r\n date: Date.now(),\r\n});\r\nwindow.parent.postMessage(message, '*');<\/pre>\n\u4f7f\u7528 JSON.stringify \u65b9\u6cd5\u5bf9\u5185\u5bb9\u8fdb\u884c\u5e8f\u5217\u8bdd\uff0c\u5373\u53ef\u5728\u4f20\u5165 postMessage \u65b9\u6cd5\u3002<\/p>\n
\u7236\u7ea7\u9875\u9762\u5411 iframe \u53d1\u9001\u6d88\u606f<\/strong><\/div>\n\u5728\u7236\u7ea7\u9875\u9762\uff0c\u4f7f\u7528 iframe.contentWindow \u8c03\u7528 postMessage \u65b9\u6cd5\uff0c\u5373\u53ef\u53d1\u9001\u6d88\u606f\u7ed9 iframe\u3002<\/p>\n
iframeEl.contentWindow.postMessage(message, '*');<\/pre>\niframeEl \u8868\u793a iframe DOM \u5bf9\u8c61\u3002<\/p>\n
\u901a\u8fc7\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u4fe1\u606f\u3002<\/p>\n
\u5411\u8c01\u53d1\u9001\u6d88\u606f\uff0c\u90a3\u4e48\u8c03\u7528\u8005\u5bf9\u8c61\u5c31\u662f\u8fd9\u4e2a\u53d1\u9001\u6d88\u606f\u7684\u76ee\u6807\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5f53\u524d\u5bf9\u8c61\u3002<\/p>\n
\u8fd9\u4e00\u70b9\u9700\u8981\u975e\u5e38\u6ce8\u610f\uff0c\u8fd9\u91cc\u662f\u5f88\u5bb9\u91cc\u8e29\u5751\u7684\u3002<\/p>\n
\u63a5\u6536\u6d88\u606f<\/strong><\/div>\n\u65e0\u8bba\u662f\u5728 iframe \u9875\u9762\u8fd8\u662f\u7236\u7ea7\u9875\u9762\uff0c\u90fd\u662f\u4f7f\u7528 window \u76d1\u542c message \u4e8b\u4ef6\u63a5\u6536\u6d88\u606f\u3002<\/p>\n
window.addEventListener('message', function (e) {\r\n \/\/ \u83b7\u53d6\u6d88\u606f\u5185\u5bb9 data\r\n const { data } = e;\r\n});<\/pre>\n\u5982\u679c\u6d88\u606f\u5185\u5bb9\u662f\u5e8f\u5217\u5316\u540e\u7684\u5bf9\u8c61\uff0c\u8fd8\u9700\u8981\u5c06\u6d88\u606f\u5185\u5bb9\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n
window.addEventListener('message', function (e) {\r\n \/\/ \u83b7\u53d6\u6d88\u606f\u5185\u5bb9 data\r\n let { data } = e;\r\n data = JSON.parse(data);\r\n});<\/pre>\n\u4f7f\u7528 JSON.parse \u65b9\u6cd5\u5bf9\u5185\u5bb9\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\uff0c\u5373\u53ef\u7684\u5230\u4f20\u9012\u8fc7\u6765\u7684\u5185\u5bb9\u5bf9\u8c61\u3002<\/p>\n
\u6307\u5b9a\u53d1\u9001\u6d88\u606f\u7684\u57df\u540d<\/strong><\/div>\n\u4e0a\u9762\u6211\u4eec\u4f7f\u7528 postMessage \u65b9\u6cd5\uff0c\u4f20\u9012\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u90fd\u662f *\uff0c\u8fd9\u91cc\u7684\u542b\u4e49\u662f\u6307\u4efb\u4f55\u57df\u540d\u90fd\u80fd\u63a5\u6536\u6d88\u606f\u3002<\/p>\n
\u5efa\u8bae\u5982\u679c\u77e5\u9053\u6d88\u606f\u63a5\u6536\u65b9\u7684\u57df\u540d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8bf7\u4f20\u9012\u6d88\u606f\u63a5\u6536\u65b9\u7684\u57df\u540d\u3002\u56e0\u4e3a\u8fd9\u91cc\u662f\u4f1a\u5b58\u5728\u5b89\u5168\u9690\u60a3\u7684\uff0c\u4efb\u4f55\u7ad9\u70b9\u90fd\u53ef\u4ee5\u5411\u4f60\u7684\u7ad9\u70b9\u53d1\u9001\u6d88\u606f\uff0c\u5982\u679c\u6ca1\u6709\u505a\u76f8\u5173\u5b89\u5168\u5904\u7406\uff0c\u662f\u5f88\u5bb9\u6613\u9020\u6210\u653b\u51fb\u7684\u3002<\/p>\n
iframeEl.contentWindow.postMessage(message, 'https:\/\/www.baidu.com');<\/pre>\n\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u6807\u8bc6\u4ec5\u5f53 iframe \u7684\u6307\u5411 https:\/\/www.baidu.com \u65f6\u624d\u4f1a\u53d1\u9001\u6d88\u606f\u3002<\/p>\n
\u901a\u8fc7\u5236\u5b9a\u57df\u540d\u7684\u7684\u65b9\u5f0f\uff0c\u907f\u514d\u5b89\u5168\u9690\u60a3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u9875\u9762\u4e0e iframe \u8fdb\u884c\u901a\u4fe1\uff0c\u5b9e\u9645\u5de5\u4f5c\u4e2d\u53ef\u80fd\u5f88\u96be\u9047\u5230\uff0c\u4f46\u4e00\u65e6\u9047\u5230\u4e86\uff0c\u6211\u4eec\u8981\u80fd\u591f\u7acb\u5373\u60f3\u5230\u600e […]<\/p>\n","protected":false},"author":1479,"featured_media":240847,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-251332","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=251332"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251332\/revisions"}],"predecessor-version":[{"id":251334,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251332\/revisions\/251334"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/240847"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=251332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=251332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=251332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}