参考文档地址:https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessage
原理基于postMessage。
demo例子:
parent.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>父亲页面</h1>
<button onclick="sendMeg()">发送数据</button>
<iframe src="children.html" width="100%" height="200px" id="child"></iframe>
</body>
<script>
function sendMeg(){
var iframe = document.getElementById("child")
iframe.contentWindow.postMessage('父页面传送的数据','*');
}
function callback(){
console.log(回调)
}
// 监听传送到父页面的数据
window.addEventListener('message',function(data){
console.