同服务器、同域名下才可以操作,不能更改别人的网页
iframe内页:
父页面操作子页面:contentWindow
子页面操作父页面:window.top(找到最顶级的父页面)/parent(第一父页面)
新窗口页:
父页面操作子页面:window.open
子页面操作父页面:window.opener
这是父文档
<input type = "button" id = "btn" value = "changeColor">
<br>
<hr>
<iframe id = "iframe" src = "iframe2.html" frameborder = "0"></iframe>
btn.onclick = function(){
iframe.contentWindow.document.body.style.background = "purple";
}
这是子页面
<iframe src = "" frameborder = "0"></iframe>
document.onclick = function(){
parent.document.body.style.background = "skyblue";
}
<input type="button" id = "btn1" value = "打开一个窗口">
<input type="button" id = "btn2" value = "操作新窗口">
let opener;
btn1.onclick = function(){
opener = open("iframe4.html");
}
btn2.onclick = function(){
opener.document.body.style.background = "yellow";
}
新打开的窗口
document.onclick = function(){
opener.document.body.style.background = "red";
close();
}