如何从父级向 iframe 发送数据

问题描述 投票:0回答:1

我有一个问题。早些时候,我很难从 iframe 向父节点发送

postMessage
。幸运的是,我解决了它,因为不知何故
window.parent.postMessage
不起作用,但 top.postMessage 起作用了。现在我遇到了相反的问题。我想从父级向 iframe 发送消息
myIframe.contentWindow.postMessage
但它不起作用。还有什么我可以尝试的吗?

我正在本地主机中进行测试,一个在本地主机:3000,另一个在本地主机:9001

javascript iframe postmessage
1个回答
0
投票
const iframe = document.getElementById("myFrame");
const iframeElement = iframe.contentWindow.document.getElementsByTagName("H1")[0];
// Do something with `iframeElement`

参考:https://www.w3schools.com/howto/howto_js_element_iframe.asp

© www.soinside.com 2019 - 2024. All rights reserved.