我有同样的问题,但迄今为止所提供的解决方案并没有解决这个问题对我来说。在我看来,现有的解决方案并不直接回答这个问题。所以这是我的解决方案。
重述问题:在父页面中,我想监听子页面触发的事件。
解决方案:解决这一神秘的关键是认识到,在特定窗口中运行的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.
});
});
你能解释你如何替换绑定到该事件。 – user1736947 2014-07-15 21:35:35
请解释如何实际执行绑定。 – 2015-02-15 15:23:22