子页面
<body>
子级:B页面<br/>
<button id="b_button">B页面发送A页面数据</button><br/>
<script>
document.getElementById("b_button").onclick = function(){
var param = {'name':'admin'};
window.parent.postMessage(param,'*');
}
</script>
</body>
父页面
<body>
父级:A页面<br/><br/>
<iframe src="http://www.genetek.cc/iframe/b.html" width="500px" height="200px" id="iframe"></iframe>
<script>
window.addEventListener('message', function(e) {
alert(JSON.stringify(e.data));
console.log('获取子级B页面返回值:');
console.log(e.data);
})
</script>
</body>
本文介绍了一个使用postMessage进行跨窗口通信的例子,展示了如何从子页面(B页面)向父页面(A页面)发送数据,并在父页面中接收并显示这些数据。
2470

被折叠的 条评论
为什么被折叠?



