[英]How to get the parent iframe's parent iframe using JavaScript?
So I have a page index.html
with an iframe: 所以我有一个带有iframe的页面
index.html
:
<iframe src="iframe1.html" width="500" height="400"></iframe>
In iframe1.html
I have another nested iframe: 在
iframe1.html
我还有另一个嵌套的iframe:
<iframe src="iframe2.html" width="500" height="400"></iframe>
In iframe2.html
I have this code: 在
iframe2.html
我有以下代码:
<script>
window.onload = function() {
document.getElementById('change-color').onclick = function() {
window.parent.document.body.style.backgroundColor = "red";
return false;
};
}
</script>
<a href="" id="change-color">Change Color</a>
What happens now when you click the "change color" link is the iframe's parent page iframe1.html
gets a red background color. 现在,当您单击“更改颜色”链接时,iframe的父页面
iframe1.html
将获得红色背景色。 Instead I want parent's parent page index.html
to get the color change. 相反,我希望父级的父页面
index.html
获得颜色更改。 How to access that? 如何访问?
Use window.parent.parent
to get the grandparent window. 使用
window.parent.parent
获取祖父母窗口。 So you want: 所以你要:
window.parent.parent.document.body.style.backgroundColor = "red";
And if you want the top-most parent, no matter how many levels deep you are, you can use window.top
. 而且,如果您想要最顶层的父级,则无论您有多深,都可以使用
window.top
。 See 看到
Find most upper parent window with javascript 使用JavaScript查找最上层的父窗口
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.