簡體   English   中英

如何使用JavaScript獲取父iframe的父iframe?

[英]How to get the parent iframe's parent iframe using JavaScript?

所以我有一個帶有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獲取祖父母窗口。 所以你要:

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

而且,如果您想要最頂層的父級,則無論您有多深,都可以使用window.top 看到

使用JavaScript查找最上層的父窗口

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM