且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在父页面的iframe中调用函数?

更新时间:2023-09-01 11:24:28

只要框架页位于同一域(或子域,并且您正在设置document.domain)上,就需要访问frame元素的 contentWindow 属性.例如:

As long as the framed page is on the same domain (or on a sub-domain, and you're setting document.domain), you need to access the contentWindow property of the frame element. For example:

$("#myFrame")[0].contentWindow.myFunction();

// or, if jQuery hasn't made you lazy 
document.getElementById("myFrame").contentWindow.myFunction();

大多数浏览器还支持 contentDocument ,但Internet Explorer不支持.如果您的框架页面位于其他域中,则会出现访问被拒绝"错误.

Most browsers also support contentDocument, but Internet Explorer doesn't. If your framed page is on a different domain then you'll get an Access Denied error.