如何在没有插件的情况下实现Jquery中的“观察者”?

时间:2021-02-06 14:29:23

I need a implementation of http://www.prototypejs.org/api/event/observe in Jquery? With "frequency" too?

我需要在Jquery中实现http://www.prototypejs.org/api/event/observe吗?“频率”吗?

I need to a ticket for cakephp.

我需要一张cakephp的机票。

Thanks, Celso.

谢谢,Celso。

2 个解决方案

#1


5  

Prototype observe is equivalent to jquery bind

观察原型等同于jquery绑定

so something like this in prototype:

原型中有这样的东西

$('myElement').observe('click', function(){...});

would be equivalent to this in jquery:

在jquery中相当于:

$('#myElement').bind('click', function(){...});

The actual implementation in the library is different, but this will provide a similar result. Also, in jquery you won't have to attach a bind() function on the end of your handler as jquery binds the scope automatically.

库中的实际实现是不同的,但是这将提供类似的结果。此外,在jquery中,当jquery自动绑定范围时,您不必在处理程序的末尾附加bind()函数。

#2


3  

in addition to

除了

$('#myElement').bind('click', function(){...});

also take a look at

再来看看

$(document).on('click', '#myElement', function(){...}); # jquery >= 1.7

which binds a click event to all present and even future elements - especially usefull if you want multiple objects to react on clicks or which are added dynamically. See http://api.jquery.com/on/ for a more detailed explanation.

它将单击事件绑定到所有当前甚至将来的元素——特别是如果您希望多个对象对单击作出响应或动态添加哪些对象,则使用usefull。请参阅http://api.jquery.com/on/获得更详细的解释。

#1


5  

Prototype observe is equivalent to jquery bind

观察原型等同于jquery绑定

so something like this in prototype:

原型中有这样的东西

$('myElement').observe('click', function(){...});

would be equivalent to this in jquery:

在jquery中相当于:

$('#myElement').bind('click', function(){...});

The actual implementation in the library is different, but this will provide a similar result. Also, in jquery you won't have to attach a bind() function on the end of your handler as jquery binds the scope automatically.

库中的实际实现是不同的,但是这将提供类似的结果。此外,在jquery中,当jquery自动绑定范围时,您不必在处理程序的末尾附加bind()函数。

#2


3  

in addition to

除了

$('#myElement').bind('click', function(){...});

also take a look at

再来看看

$(document).on('click', '#myElement', function(){...}); # jquery >= 1.7

which binds a click event to all present and even future elements - especially usefull if you want multiple objects to react on clicks or which are added dynamically. See http://api.jquery.com/on/ for a more detailed explanation.

它将单击事件绑定到所有当前甚至将来的元素——特别是如果您希望多个对象对单击作出响应或动态添加哪些对象,则使用usefull。请参阅http://api.jquery.com/on/获得更详细的解释。