谈谈contentWindow 和contentDocument以及 iframe

1,contentWindow:是用来获取子窗口的window对象的,它兼容各大浏览器,用法如下

document.getElementById("iframeId").contentWindow 

这样简单的一句就得到了iframe包含页面的window对象;

2,contentDocument:是用来获取子窗口的document对象的,主流浏览器都支持和ie8+支持,用法如下

document.getElementById("iframeId").contentDocument

这样简单的一句就得到了iframe包含页面的document对象;

以上两种方法是在父窗口中获取到子窗口,既然我们都能拿到子窗口window对象和document对象了,那么子窗口内其他的操作就easy了 !

如果要通过子窗口A访问到同级的子窗口B,那么我们可以在子窗口A中这么来做:

parent.getElementById("iframeId").contentWindow.document.getElmentById("frameId_B")  

或者
parent.getElementById("iframeId").contentDocument.getElmentById("frameId_B")

就得到B窗口了。

此外,我们知道在iframe的页面中如果使用history.back(),那么会使其主窗口后退。而通常我们不希望这样,于是你可以在子窗口中采用
parent.document.getElementById('iframe id').contentWindow.history.back(); 

来优雅的实现

开心一刻:

原文地址:https://www.cnblogs.com/wenber/p/3628767.html