同域名下iframe父子页面间的通信

iframe元素的获取方式:
1. 获取iframe的window对象
    iframe.contentWindow

2. 获取iframe的document对象
    iframe.contentDocument
  1. 父页面调用子iframe页面

1. 使用contentWindow属性
document.getElementById('iframeId').contentWindow.document.getElementById('子页面中的元素ID');

2. 通过iframe的name(名字)直接获取
iframeName.window.document.getElementById('子页面中的元素ID');

3. 通过window对象的frames[]数组对象直接获取子frame对象
window.frames[0].document.getElementById('子页面中的元素ID');
或者
window.frames["iframeName"].document.getElementById('子页面中的元素ID');

  1. 子iframe页面调用父页面
1. 通过parent对象获取父页面的window对象内元素及方法
parent.window.document.getElementById('');
2. 通过top对象获取父页面的window对象内元素及方法
top.window.document.getElementById();
  1. 主页面内兄弟iframe页面之间相互调用




1. 通过兄弟iframe的ID获取其dom,然后通过内置属性contentWindow取得window对象

parent.window.document.getElementById().contentWindow.document.getElementById()

2. 通过iframe的名字直接获取子窗口的window对象
parent.window.iframe2Name.window.document.getElementById()

3. 通过window对象的frames[]数组对象直接获取子iframe对象
parent/top.frames[2].document.getElementById()
或
parent.window.frames['iframe3Name'].document.getElementById()

你可能感兴趣的:(html,js,html,javascript)