这似乎是Javascript事件的一个类。它是什么? [英] This appears to be a class on a Javascript event. What is it?

查看:79
本文介绍了这似乎是Javascript事件的一个类。它是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚遇到一些看起来像这样的jQuery:

I just ran across some jQuery that looks like this:

$('.add-row').live('click.add', function() { 
    // do something
}

这似乎与'click.add'事件绑定。我自己使用自定义事件并认为它们很棒,但是要做 git grep 在我们的代码库中没有显示触发自定义事件名为 click.add 的任何地方,并且在任何情况下,此行为都是由正常点击触发的。我也没在HTML中的任何地方看到 .add 类。

This appears to be binding to the 'click.add' event. I use custom events myself and think they're awesome, but doing git grep on our code base doesn't reveal any place where a custom event called click.add is triggered, and in any case, this behavior is triggered by a normal click. Nor do I see an .add class anywhere in the HTML.

我认为你不能上课关于Javascript事件。知道这个奇怪的语法是什么吗?

I don't think you can have classes on Javascript events. Any idea what this odd bit of syntax is?

推荐答案

这是一个名为命名空间事件。在此示例中, add 是一个命名空间。它实际上是一个事件类,以便您可以分类它们并相应地处理/触发它们。例如,您可以编写一个插件并为每个事件处理程序提供 myPlugin 的命名空间,以便您可以在不删除用户的其他事件处理程序的情况下取消绑定它们:

This is a featured called namespaced events. In this example, add is a namespace. It is effectively a class for events, so that you can categorise them and handle/trigger them accordingly. For instance, you might write a plugin and give every event handler a namespace of myPlugin so that you can unbind them without removing the user's other event handlers:

$('a').bind('click.myPlugin', function(){ /*...*/ }); // bind with the myPlugin namespace
$('a').bind('click'), function() { /* ... */ }); // bind without a namespace
$('a').unbind('.myPlugin'); // only removes the first function

这适用于触发器以及。

这篇关于这似乎是Javascript事件的一个类。它是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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