(1)子页面调用父页面的方法或者变量:
window.parent. 方法 () 或者变量名 window.parent 相当于定位到父页面 之后的操作和在父页面中写代码一样写
- window.parent.aa();// 调取 aa 函数
- window.parent.bb;// 调取 bb 变量
例如: 想在子页面中得到 id 为 aaa 的文本框的值
- window.parent.$("#aaa").val();// 这种写法的前提是引用了 jquery
- window.parent.getElementById("aaa").value; //js 的写法
(2)父页面调取子页面 主要是通过 contentWindow 定位到子页面
- document.getElementById("childframe").contentWindow.childtest();
- // 调取子页面中的 childtest 方法 js 的写法
- var childWindow = $("#addFrame")[0].contentWindow;// 获取子窗体中的对象
- childWindow.formSubmit();// 调取子页面的 formSubmit 方法 jquery 的写法
- // 注释: 其中 childframe 和 addFrame 都时 iframe 的 id
来源: https://www.2cto.com/kf/201808/774803.html