在非dom元素上使用jquery on / off和触发器方法的缺陷是什么 [英] what is the pitfall in using jquery on/off and trigger method on non-dom elements

查看:121
本文介绍了在非dom元素上使用jquery on / off和触发器方法的缺陷是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jquery开/关方法和触发器在非dom元素/常规js对象上执行是否存在潜在的缺陷。

Is there a potential pitfall in using jquery on/off method and trigger to execute on non-dom elements/regular js objects.

谢谢

推荐答案

不,你可以使用 .on .off 对常规对象,只要你记住它会为对象添加额外的属性。

No, you can use .on and .off on regular objects, as long as you keep in mind that it will add additional properties to the object.

var obj = {};

$(obj).on("mycustomevent",function(){
  alert("mycustomevent triggered");
  $(this).off("mycustomevent").trigger("mycustomevent");
}).trigger("mycustomevent"); // fires off one alert.

我必须问,在什么情况下将事件绑定到对象是有意义的?为什么不给对象一个可以调用的方法呢?

I must ask though, in what situation does it make sense to bind events to an object? why not just give the object a method that you can call?

这篇关于在非dom元素上使用jquery on / off和触发器方法的缺陷是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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