简体   繁体   English

jQuery $(window).blur vs native window.onblur

[英]jQuery $(window).blur vs native window.onblur

What are the advantages of using jQuery's 使用jQuery有什么好处

$(window).blur(function() { ... })

to attach an event handler versus setting it directly with 附加事件处理程序与直接设置它

window.onblur = function() { ... }

It seems that the latter is less robust because it only supports one blur handler, and when used with other packages, other code might override the window.blur value with another function. 似乎后者不太健壮,因为它只支持一个模糊处理程序,并且当与其他包一起使用时,其他代码可能会用另一个函数覆盖window.blur值。 However, couldn't this also happen with the jQuery implementation too, which presumably uses window.blur as its underlying implementation? 但是,这也不会发生在jQuery实现上,这可能会使用window.blur作为其底层实现吗?

EDIT: Several people have also mentioned the window.addEventListener alternative, which can be used to add an 'onblur' event apart from the methods above. 编辑:有些人还提到了window.addEventListener替代方法,除了上面的方法之外,它还可用于添加'onblur'事件。

$(window).blur(function() { ... })

Lets you add one or more event handlers. 允许您添加一个或多个事件处理程序。


window.onblur = function() { ... }

Lets you only have one event handler handling the blur event. 让您只有一个处理模糊事件的事件处理程序。


The former uses the jQuery's own event handle mechanism. 前者使用jQuery自己的事件句柄机制。 The call to .blur() will delegate to jQuery.fn.on() which in turn will delegate to jQuery.event.add . .blur()的调用将委托给jQuery.fn.on() ,后者将委托给jQuery.event.add This add() method will create it's own handler for the given event type and tell addEventListener() to call this handler whenever a event of given type is fired. 这个add()方法将为给定的事件类型创建它自己的处理程序,并告诉addEventListener()在触发给定类型的事件时调用此处理程序。 So basically jQuery has it's own way of event handling which relies on addEventListener() to execute properly. 所以基本上jQuery有自己的事件处理方式,它依赖于addEventListener()来正确执行。

The latter is just an attribute which can only contain one value so queueing event handlers is impossible. 后者只是一个只能包含一个值的属性,因此排队事件处理程序是不可能的。

I wrote a little demonstration to prove this point: http://jsfiddle.net/GnNZm/1/ 我写了一个小演示来证明这一点: http//jsfiddle.net/GnNZm/1/

With the jQuery method, you can attach multiple event handlers. 使用jQuery方法,您可以附加多个事件处理程序。 By setting window.onblur , you can only have a single handler. 通过设置window.onblur ,您只能拥有一个处理程序。

Pure JavaScript also has this: window.addEventListener() . 纯JavaScript也有这个: window.addEventListener() In fact, i'm sure jQuery uses this internally. 事实上,我确信jQuery在内部使用它。 ( Yes they do .) 是的,他们这样做 。)

(EDIT) The window.onblur property is basically a shortcut for setting a single handler. (编辑) window.onblur属性基本上是设置单个处理程序的快捷方式。 Using addEventListener() (or the jQuery wrapper) basically creates a list of event handlers, which all get fired when the event happens. 使用addEventListener() (或jQuery包装器)基本上创建了一个事件处理程序列表,这些事件在事件发生时都会被触发。 I haven't tested, but i think you can even use the two together. 我没有测试过,但我认为你甚至可以将两者结合使用。 Because it's a list, not a single value, multiple handlers shouldn't interfere with each other. 因为它是一个列表而不是单个值,所以多个处理程序不应相互干扰。 They can also be removed separately or all at once. 它们也可以单独移除或一次移除。

jQuery's event handlers, using on() , also let you namespace your handlers, to prevent clashes if a plugin removes its handlers. jQuery的事件处理程序,使用on() ,也可以让你的处理程序命名空间,以防止插件删除其处理程序时发生冲突。 Pure JS doesn't seem to have this easily. 纯JS似乎没有这么容易。

For jquery blur 对于jquery模糊

The blur event does not bubble in Internet Explorer. 模糊事件在Internet Explorer中不会出现气泡。 Therefore, scripts that rely on event delegation with the blur event will not work consistently across browsers. 因此,依赖于使用模糊事件的事件委派的脚本将无法在浏览器中一致地工作。 As of version 1.4.2, however, jQuery works around this limitation by mapping blur to the focusout event in its event delegation methods, .live() and .delegate(). 但是,从版本1.4.2开始,jQuery通过将模糊映射到其事件委托方法.live()和.delegate()中的focusout事件来解决此限制。

taken from jquery doc https://api.jquery.com/blur/ 取自jquery doc https://api.jquery.com/blur/

Also jquery allows you bind multiple event handlers 此外,jquery允许您绑定多个事件处理程序

When you attach an event there is the possibility of overwriting an event already attached to an event handler. 附加事件时,可能会覆盖已附加到事件处理程序的事件。 This used to happen a lot with window.onload() where different scripts overwrote each others event handlers. 这通常发生在window.onload()中,其中不同的脚本覆盖了彼此的事件处理程序。

eg: 例如:

//lightbox.js
window.onload = function() { /* do lightbox stuff */ }

//carousel.js 
window.onload = function() { /* do carousel stuff */ }

So the common practice used to be something like this: 所以常见的做法是这样的:

var existing_event_handlers = window.onload;
window.onload = function(){

    //my event code
    alert('onready fired');

    //call other event handlers after
    existing_event_handlers();
} 

Using window.onblur = function() { ... } still has an advantage because you can specifically dictate if you want your event fired before or after other attached events. 使用window.onblur = function() { ... }仍然有一个优势,因为您可以专门指定您是否希望在其他附加事件之前或之后触发事件。

Like many other answers already pointed out jQuery abstracts you from most browser differences. 像许多其他已经指出的答案一样,jQuery从大多数浏览器差异中抽象出来。 Version before IE9 used attachEvent() rather than addEventListener(). IE9之前的版本使用了attachEvent()而不是addEventListener()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM