背景
頁面A透過iframe巢狀頁面B,兩個頁面埠不同,想要從頁面A點選事件呼叫頁面B的方法。
實現
頁面A
主要應用
postMessage
方法,第一個引數是將要傳送到其他window的資料,第二個引數是指定哪些視窗能接收到訊息事件,可以傳字串 “*” 表示無限制,或者是一個URI。
window。addEventListener(“click”, e => { console。log(“向iframe子元件傳送”); document。getElementById(“child”)。contentWindow。postMessage(“測試”, “*”); });
頁面B
window。addEventListener(“message”, function(event) { console。log(“iframe收到”); console。log(event) that。setSeen = false; that。$nextTick(() => { that。$forceUpdate(); }); }, false);
測試
奧利給了,兄弟們
跨域傳值成功