iframe JS 操作父级窗口

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>父级窗口</title>
</head>
<body style="background-color: #000;">
<iframe src="http://127.0.0.1:8081/bbb.html" width="500" height="500" border="0"></iframe>
<script type="text/javascript">
window.addEventListener('message',function (e) {
console.log(e)
})
</script>
</body>
</html>

 

/*******************************************/

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>子窗口</title>
</head>
<body style="background-color: #FF0000;">
<button id='aaa'>clear</button>
<script type="text/javascript">
document.getElementById('aaa').onclick = function () {
window.parent.postMessage({
'aaa':'bbb'
},'*')
}
</script>
</body>

 

posted @ 2016-09-22 10:08  mrt_yy  阅读(469)  评论(0编辑  收藏  举报