自定义命名空间事件将不起作用 [英] Custom namespaced events will not work

查看:79
本文介绍了自定义命名空间事件将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让自定义命名空间事件按此处所述工作: http://docs.jquery.com / Namespaced_Events

I'm trying to get custom namespaced events to work as described here: http://docs.jquery.com/Namespaced_Events

但我必须遗漏一些东西,因为我无法触发事件,除非命名空间完全匹配。

But I must be missing something since I can't get events to trigger unless the namespace matches exactly.

我创建了一个演示问题的小提琴: http://jsfiddle.net/PsR6x/ 1 /

I created a fiddle to demonstrate the problem: http://jsfiddle.net/PsR6x/1/

我做错了什么?

更新

第二个绑定是在jQuery中调用的 v1.3.2 & v1.5.2 但不在 v1.6.4 及以上。
第三个绑定不会在任何版本中调用。

The second bind is invoked in jQuery v1.3.2 & v1.5.2 but not in v1.6.4 and above. The third bind is not invoked in any version.

on 而不是 bind 也不起作用。

推荐答案

事件命名空间不是层次结构。

使用您拥有的代码, $('#someid')。trigger('griffin.model'); 触发所有代码, $('#someid')。触发器('griffin.updated'); 触发所有。

Event namespaces are not a hierarchy.
With the code that you have, $('#someid').trigger('griffin.model'); triggers all, and $('#someid').trigger('griffin.updated'); triggers all.

$('body').bind('griffin.model.updated.user', function() {
    alert('Exact namespace = trigger');
});
$('body').bind('griffin.model.updated', function() {
    alert('Will not trigger :(');
});
$('#someid').bind('griffin.model.updated', function() {
    alert('Same item, but not the same namespace = wont trigger');
});

这里你基本上为第一个创建了三个独立的命名空间,为第二个创建了两个独立的命名空间第三。


仔细查看这个例子并阅读我留给你的评论。尝试许多不同的事情,看看你可以做的不同的事情,有时很有用。

Here you have essentially created three separate namespaces for the first, and two separate namespaces for the second and third.

Look carefully at this example and read the comments I left for you. It's sometimes useful to try many different things, to see the different things you can do.

关于 on( )不工作:

这个片段直接取自jquery-1.7.1.js:

In regards to on() not working:
This snippet is taken directly from jquery-1.7.1.js:

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
}

如您所见, bind()简直就是 on()的包装器,应该完全相同,但 bind()不支持选择器或委托。

As you can see, bind() is simply a wrapper for on(), and should work exactly the same, with the exception of bind() not supporting selectors or delegation.

这篇关于自定义命名空间事件将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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