此例采用 iframe
父页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>iframe父级页面</title>
<style>
* {
padding: 0;
margin: 0;
}
iframe {
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<h2>我是父级页面</h2>
<button id='btn'>父页面的按钮</button>
<div id="default">div内容</div>
<iframe src="http://tstwebportal.u-breath.cn/pfmsc-web/pages/records.html" frameborder="0" name='myframe' id='myframe'></iframe>
<script language="javascript" type="text/javascript">
window.addEventListener('message',function(e){
console.log(e.data);
if(e.data.msg==='hideselfService'){
document.getElementById('default').innerHTML = '子页面点击了';
}
});
</script>
</body>
</html>
iframe页面
console.log('点击了possMessage')
parent.postMessage( {msg: 'MessageFromIframePage'}, '*');