开发者

How implement a "observer" in Jquery without plugins?

I need a implementation of http://www.prot开发者_JS百科otypejs.org/api/event/observe in Jquery? With "frequency" too?

I need to a ticket for cakephp.

Thanks, Celso.


Prototype observe is equivalent to jquery bind

so something like this in prototype:

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

would be equivalent to this in 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜