开发者

jQuery design mistake or historical reason?

开发者 https://www.devze.com 2022-12-11 07:36 出处:网络
Today is jQuery day. I found this in the documentation: blur()Returns: jQuery Triggers the blur event of开发者_运维百科 each matched element.

Today is jQuery day. I found this in the documentation:

blur()   Returns: jQuery Triggers the blur event of开发者_运维百科 each matched element.  
blur(fn) Returns: jQuery Bind a function to the blur event of each matched 
                  element.

In other words, the behavior of the function is totally different depending if it accepts or not an argument.

Is this a design mistake or there's a historical reason for this ?

Keep into account that I know nothing about javascript nor jQuery, and I am trying to get a feeling of it.


That's how jQuery is designed, it's the same for all events. To add a handler to an element you use e.blur(function(){...}), and to trigger the event, you use e.blur(). It kind of makes sense, you just have to get used to it.


Definitely not a design mistake, because it goes for multiple events, such as click, however you should be using .trigger('blur')

It makes sense because .blur() or .click() by nature invokes the event handlers attached to the specific event, and all .blur(fn) does is bind it to .bind('event') where behind the scenes it registers the event handlers.

0

精彩评论

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