且构网

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

如何从父级调用iframe函数?

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

您可以将功能对象从iframe传递到父函数,然后从那里调用.

You can pass the function object from iframe to parent function and invoke from there.

在父项上:

function tunnel(fn){
     fn('postMe');
}

在iframe上:

function myFunction(postMe) {
     ((console&&console.log)||alert)("postMe= " + postMe);
}
window.parent.tunnel(myFunction);

如果此时不会加载父文档,请尝试使用下面的

If your parent document will not be loaded at that point, try using below

var parentDocument = window.parent.document; 
$( parentDocument ).ready(function() { 
     window.parent.tunnel(myFunction); 
}