一, 相互调用方法
子级页面调用父级页面的方法:
Windows.parent.parentFunction();
parentFunction 为父级页面的自定义函数
父级页面调用子级页面的方法:
document.getElementById("iframeID").contentWindow.childFunction();
iframeID 为目标 iframe 的 id,childFunction 为子级页面的自定义函数
二, 相互操作 dom
子级页面操作父级页面 dom:
Windows.parent.document.getElementById("parentID").style.color='yellow';
父级页面操作子级页面 dom:
document.getElementById('iframeID').contentDocument.getElementById('childID').style.color='red'
iframeID 为目标 iframe 的 id,childID 为子级页面的 dom 的 id
注意: 只有当 iframe 加载完之后才可以父级操作子级 dom, 否则会报错, 可以尝试 Windows.onload 来解决此问题.
来源: http://www.jianshu.com/p/3ddfce32904b