iframe子父页面函数互相调用

1、iframe子页面调用父页面js函数 

子页面调用父页面函数只需要写上window.praent就可以了。比如调用a()函数,就写成: 


window.parent.a(); 


子页面取父页面中的标签中的值,比如该标签的id为“test”,则: 


window.parent.document.getElementById("test").value; 

jQuery方法为: 

$(window.parent.document).contents().find("test").val(); 


但是在chrome浏览器下此方法无效!因为在chrome 5+中,window.parent无法在file://协议中运行,但是发布了之后http://协议下是可以运行的。此方法支持ie、firefox浏览器。 

2、iframe父页面调用子页面js函数 


这个就稍微复杂一些,下面的方法支持ie和firefox浏览器: 

document.getElementById('ifrtest').contentWindow.b(); 

子页面取父页面中的标签中的值,比如该标签的id为“test”,则: 

document.getElementById("test").value; 


注:ifrtest是iframe框架的id,b()为子页面js函数。contentWindow属性是指定的frame或者iframe所在的window对象,IE下可以省略。

原文地址:https://www.cnblogs.com/dongtianqi/p/6598430.html