在子窗口上绑定jQuery事件 [英] Binding jQuery event on a child window

查看:65
本文介绍了在子窗口上绑定jQuery事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,该页面包含以下javascript:

I have a page that has the following javascript:

var w = window.open("somePage.html", '', 'width=500, height=500');
$(w).bind('someEvent', function() { alert('I see the event!'); });

,并且在 somePage.html 上,我尝试触发该事件:

and on somePage.html I attempt to trigger the event:

$(window).trigger('someEvent', '');

该事件不会触发(但是,如果我执行事件绑定并在同一页面上触发,则会触发该事件).我尝试了各种绑定方式,例如使用 this this.window

The event doesn't fire (it does however if I do the event binding and trigger on the same page). I have tried all sorts of variations of binding such as using this and this.window

我是不是从绑定端引用了正确的对象?

Am I somehow not referencing the right object from the binding side?

(我正在Chrome和Firefox中对此进行测试)

(I am testing this in Chrome and Firefox)

大卫·罗德里格斯(David Rodrigues)很友善地创建了这个问题的答案: http://jsfiddle.net/KARgF/& http://fiddle.jshell.net/vTQ9U/

David Rodrigues was kind enough to create a jsfiddle of this question: http://jsfiddle.net/KARgF/ & http://fiddle.jshell.net/vTQ9U/

推荐答案

我认为问题在于$仍引用主机窗口中的jquery.如果您这样做:

I think the problem is that $ still refers to the jquery in the host window. If you do this:

var w = window.open("somePage.html", '', 'width=500, height=500');
var $ = w.$;
$(w).bind('someEvent', function() { alert('I see the event!'); });

你应该很好.

这篇关于在子窗口上绑定jQuery事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆