1.情景展示

如何通过JS操作iframe里面的内容?

<!-- chart图表 -->
<iframe id="myframe" src="" height="100%" width="100%"></iframe>

2.通过父页面操纵子页面

获取iframe页面中的元素

操作

javascript

jQuery

方式一

document.getElementById("myframe").contentWindow.document.getElementById("V_ORGID").value = 111;

$("#myframe").contents().find("#V_ORGID").val('111');

方式二

document.frames["myframe"].document.getElementById("V_ORGID").value = 111;

$("#V_ORGID",document.frames("myframe").document).val('111');

调用iframe页面中的方法

操作

javascript

jQuery

方式一

document.getElementById("myframe").contentWindow.test();


方式二

document.frames["myframe"].test();


2.通过子页面操纵父页面

获取iframe页面中的元素

操作

javascript

jQuery

方式一

window.parent.document.getElementById("ORGID").value = 111;

$("#ORGID",parent.document).val('111');

调用iframe页面中的方法

操作

javascript

jQuery

方式一

window.parent.test2();


2022年5月17日10:48:12

说明:如果有多个父页面,可通过元素下标,精准获取到当前页面的父页面。

window.parent[4].document.getElementById("TABLENAME").value = TABLENAME;

updateTime--2018年8月23日19点14分

3.通过子页面操纵父页面中的其它iframe页面

iframe必须设置了id。

  获取iframe页面中的元素

window.frames['id'].document.getElementById();

  调用iframe页面中的方法

window.frames['id'].test();

 

写在最后

  哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

作者:Marydon