1 回答
TA贡献1802条经验 获得超5个赞
规避此问题的正确方法是使用Window.postMessage - 本质上,将消息从 iframe 发送到其父窗口,如果它具有 的事件侦听器"message",则可以相应地做出反应。
IE。:
a.com/index.html
<!DOCTYPE html>
<html>
<body>
<script>
let someFunc = t => document.querySelector("#test").text = t;
window.addEventListener("message", event => someFunc(e.data));
</script>
<span id="test">This is some dummy text.</span>
<iframe src="https://b.com/index.html"></iframe>
</body>
</html>
b.com/index.html
<!DOCTYPE html>
<html>
<body>
<button id="btn">Test</button>
<script>
document.querySelector("#btn")
.addEventListener("click", event => {
window.parent.postMessage("Changed text", "http://a.com");
});
</script>
</body>
</html>
添加回答
举报