Jquery 绑定到子iframe中的自定义事件?

Jquery 绑定到子iframe中的自定义事件?,jquery,Jquery,我在页面上有一个子iframe,id为editor。iframe内部是一个小jquery小部件(不是小部件工厂的jquery ui小部件),它在一些用户交互之后触发一个简单的“更改”事件 我正在尝试从父页面绑定到该事件 $('#editor_iframe').contents().find('#uielement').bind('change', function(){....}); 没有运气,什么都没有。在做了一些令人毛骨悚然的研究之后,我猜iframe中的事件被绑定到该窗口内的jque

我在页面上有一个子iframe,id为editor。iframe内部是一个小jquery小部件(不是小部件工厂的jquery ui小部件),它在一些用户交互之后触发一个简单的“更改”事件

我正在尝试从父页面绑定到该事件

$('#editor_iframe').contents().find('#uielement').bind('change', function(){....});  
没有运气,什么都没有。在做了一些令人毛骨悚然的研究之后,我猜iframe中的事件被绑定到该窗口内的jquery命名空间。。。如果这是正确的,我如何在iframe窗口中访问jquery命名空间


有人吗?

埃里克,我今天遇到了类似的问题

我发现我的javascript函数在iframe加载完成之前就已经运行了。 试试这个:

$('#editor_iframe').load(function() {
  $(this)
    .contents()
    .find('#uielement')
    .bind('change', function() {
        //  do stuff
    });
});

您可以通过以下方式从父级访问子iframe中的javascript:

document.getElementById('editor_iframe').contentWindow.yourfunction()
同样,由于jquery只是一个函数,因此可以像下面这样访问iframe jquery实例:

document.getElementById('editor_iframe').contentWindow.$(yourjquery)...

我也有同样的问题,但迄今为止提供的解决方案并不能解决我的问题。在我看来,现有的解决方案并不能直接回答这个问题。这是我的解决方案

重述问题:在父页面中,我想侦听子页面触发的事件

解决方案:解决这个难题的关键是要认识到在特定窗口中运行的jQuery实例是相互隔离的。因此,当您在子对象中触发事件时,只有子对象的jQuery实例可以侦听它。解决方案是向调用父窗口中函数的子窗口添加侦听器

触发子页面中的事件:

$(document).trigger("myCustomEvent");
var $iframe = $("#theIframe");

// Wait for the iframe to finish loading...
$iframe.on("load", function () {
    // Bind an event handler to the iframe's instance of jQuery, listening for the event called "myCustomEvent".
    $iframe[0].contentWindow.$($iframe[0].contentWindow.document).on("myCustomEvent", function () {
        // The code in that you put into this anonymous function resides in and will
        // run in the context of the parent page.
    });
});
父页面中的事件处理程序代码:

$(document).trigger("myCustomEvent");
var $iframe = $("#theIframe");

// Wait for the iframe to finish loading...
$iframe.on("load", function () {
    // Bind an event handler to the iframe's instance of jQuery, listening for the event called "myCustomEvent".
    $iframe[0].contentWindow.$($iframe[0].contentWindow.document).on("myCustomEvent", function () {
        // The code in that you put into this anonymous function resides in and will
        // run in the context of the parent page.
    });
});

我建议家长和孩子之间用英语交流。它更安全、更可靠。

感谢您的回复,但我的绑定已经在加载函数中,如您所述。您能否解释一下您是如何将绑定替换到该事件的。请解释一下如何实际执行绑定的。