jQuery,为触发事件的元素findselect器

我正在开发一个相当轻量级的实时应用程序。

我们正在使用jQuery的事件系统在应用程序中的模块之间进行对话。 因此,在DOM中进行更改的所有内容都必须通过事件来实现。

这些事件被一个客户端的socket.io实现捕获,它要求nodejs socket.io服务器把事件广播给所有其他客户端。

我的问题:有没有办法找出事件触发的select器? 所以,如果我打电话

$("tag.class").trigger("myevent"); 

我可以做某事

 $(document).on("myevent", function() { console.log(this.selector) }); 

它会打印“tag.class”

这很有趣,因为知道用户在select时触发了某个事件,而不是某个元素上的某个事件,这些事件之后可能显得非常随机,这将是有趣的。

我已经读过这个问题,但我希望能够得到在应用程序中触发的所有事件的select器。

感谢您的时间 :)

从链接的答案中获取灵感,可以覆盖jQuery trigger函数。 就像是:

 $.fn.origTrigger = $.fn.trigger; $.fn.trigger = function (fn) { var selector = this.selector; this.origTrigger(function (ev) { fn(ev, selector); }); }; 

您最好的select是发送数据与事件。 这也是更强大的,因为你有更多的控制。 select器可能并不漂亮,但数据是。

 $('tag.class').trigger('myevent', ['some', 'param']); $(document).on('myevent', function(param1, param2) { console.log(param1); // "some" console.log(param2); // "param" }); 

当然,如果你想发送尽可能多的参数,你可以使用一个“选项”对象,并以这种方式处理它:

 $('tag.class').trigger('myevent', [{some: 'option', other: 'option'}]); $(document).on('myevent', function(options) { var opts = { default: 'option', some: 'default', other: 'default' }; $.extend(opts, options); console.log(opts); // {default: "option", some: "option", other: "option"} });