【问题标题】:How to get the parent iframe's parent iframe using JavaScript?如何使用 JavaScript 获取父 iframe 的父 iframe?
【发布时间】:2017-08-08 04:36:22
【问题描述】:

所以我有一个带有 iframe 的页面 index.html

<iframe src="iframe1.html" width="500" height="400"></iframe>

iframe1.html 我有另一个嵌套的 iframe:

<iframe src="iframe2.html" width="500" height="400"></iframe>

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>

当您点击“更改颜色”链接时,现在 iframe 的父页面 iframe1.html 会变为红色背景色。相反,我希望父母的父页面index.html 获得颜色变化。如何访问?

【问题讨论】:

  • 可能是window.parent.parent.document.body.style.backgroundColor?
  • @Barmar 表示感谢!我正在尝试变化,但错过了那个。如果你回答我会接受。
  • 可以使用window.parent.parent或者window.top 相关:stackoverflow.com/questions/11313045/…

标签: javascript html


【解决方案1】:

使用window.parent.parent 获取祖父窗口。所以你想要:

window.parent.parent.document.body.style.backgroundColor = "red";

如果你想要最顶层的父级,无论你有多深,你都可以使用window.top。见

Find most upper parent window with javascript

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2011-04-08
    • 2013-07-10
    • 2011-02-17
    • 2010-10-30
    • 2018-05-08
    • 2017-04-28
    相关资源
    最近更新 更多