Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iframe中激发jquery事件,在父窗口中运行回调_Jquery_Events_Iframe - Fatal编程技术网

在iframe中激发jquery事件,在父窗口中运行回调

在iframe中激发jquery事件,在父窗口中运行回调,jquery,events,iframe,Jquery,Events,Iframe,我可以从iframe触发父窗口范围内的事件并侦听,但我不能从iframe范围内的iframe触发事件并侦听父窗口中的事件。。例如,在父窗口中,我有一个iframe和一些jquery代码来侦听在其主体中触发的事件 <iframe id="myframe" src="/vf-install.php" width="500" height="500"></iframe> jQuery('#myframe').contents().find("body").on('foo',fu

我可以从iframe触发父窗口范围内的事件并侦听,但我不能从iframe范围内的iframe触发事件并侦听父窗口中的事件。。例如,在父窗口中,我有一个iframe和一些jquery代码来侦听在其主体中触发的事件

<iframe id="myframe" src="/vf-install.php" width="500" height="500"></iframe>
jQuery('#myframe').contents().find("body").on('foo',function(){
    alert('foo');
});
但这对我来说是不可接受的。原因是vf-install.php是通过iframe包含的,它是我的程序的安装程序。我正在用qUnit编写一个回归测试,它将我的安装程序包含在一个iframe中,单击install按钮,然后当安装程序完成时(触发一个事件),测试将做出断言。因此,通过iframe包含它的唯一时间是在测试期间。实际用户直接调用页面,我不希望它在不存在的父窗口上触发事件

另外,作为旁注,我尝试在调用load()时将事件侦听器附加到iframe的主体,以防这是一个与“实时查询”相关的问题(在ajax更改页面后,事件不会绑定):


你能为推送事件编写一个通用处理程序吗

function triggerEvent(eventName) {
if(underTest == true) {
parent.jQuery("body").trigger(eventName);
}
else {
    jQuery("body").trigger(eventName);
}
然后您可以在代码中调用triggerEvent方法,让您的测试将“underTest”变量设置为true,或者不使用全局变量,而是检查您的测试工具是否存在


这个解决方案感觉不太漂亮,但它可能会起作用

您可以使用绑定功能:

jQuery("#yourFrameID").bind("load", function() {
    var res = jQuery(this).contents().find('body').html().toString();
    //do something with the response
});
jQuery('#myframe').load(function(){
    // attach the event listener like above
});
function triggerEvent(eventName) {
if(underTest == true) {
parent.jQuery("body").trigger(eventName);
}
else {
    jQuery("body").trigger(eventName);
}
jQuery("#yourFrameID").bind("load", function() {
    var res = jQuery(this).contents().find('body').html().toString();
    //do something with the response
});