jQuery.on();与 JavaScript .addEventListener();
有人可以解释一下为什么事件处理程序的执行顺序会根据它们的附加方式而有所不同吗?在下面的示例中,我使用 .on()
和 .addEventListener()
方法来处理不同 DOM
元素上的特定事件。
jsfiddle:http://jsfiddle.net/etsS2/
我认为在这个具体示例,事件处理程序的执行顺序将取决于事件冒泡
- 因此从原始事件目标
开始并向上移动到>document
元素。
document.getElementById('outer').addEventListener('mouseup', function (event) {
//$('#outer').on('mouseup', function (event) {
alert('This alert should not show up!');
}, false);
如果我取消注释 on();
版本,一切都会按预期工作 - jQuery
处理事件的方式与普通 JavaScript
是否有区别?
Can someone explain me why is there a difference in the order in which the event handlers are being executed depending on how they have been attached? In the example below I'm using the .on()
and .addEventListener()
methods to handle a specific event on different DOM
elements.
jsfiddle: http://jsfiddle.net/etsS2/
I thought that in this particular example the order in which the event handlers are going to be executed will depend on the event-bubbling
- so starting from the original event target
and moving up to the document
element.
document.getElementById('outer').addEventListener('mouseup', function (event) {
//$('#outer').on('mouseup', function (event) {
alert('This alert should not show up!');
}, false);
If I uncomment the on();
version everything works as expected - is there a difference in how jQuery
handles events contrary to plain JavaScript
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在文档级别使用
.on()
时,您将等待事件一直冒泡到该点。任何中间容器的事件处理程序都已被调用。事件“冒泡”是浏览器查找向作为事件的实际原始接收者的元素的父级注册的事件处理程序的过程。它在 DOM 树中向上工作。文档级别是检查的最后级别。因此,在达到该级别之前,使用
.on()
注册的处理程序不会运行。同时,首先到达“外部”级别的另一个事件处理程序,并由浏览器执行。因此,使用
.on()
注册的处理程序中的return false;
几乎没有什么用处,就像调用event.stopPropagation()
一样。代码>.除此之外,将本机事件处理程序注册与像 jQuery 这样的库所做的工作混合在一起可能是一个非常糟糕的主意,除非您认真知道自己在做什么。就在今天不久前,提出了一个几乎与此完全相同的问题。
When you use
.on()
at the document level, you're waiting for the event to bubble all the way up to that point. The event handler for any intermediate container will already have been called.Event "bubbling" is the process by which the browser looks for event handlers registered with parents of the element that was the actual original recipient of the event. It works upwards in the DOM tree. The document level is the last level checked. Thus, your handler registered with
.on()
won't run until that level is reached. In the meantime, the other event handler at the "outer" level is reached first and it is executed by the browser.Thus, that
return false;
in the handler registered with.on()
is pretty much useless, as would be a call toevent.stopPropagation()
. Beyond that, mixing native event handler registration with the work that a library like jQuery will do is probably a really bad idea unless you seriously know what you're doing.There was a question asked almost exactly like this one just a little while ago today.
看一下
addEventListener
版本:它工作得很好,因为第三个参数是
useCapture
,它指定是否应该使用事件的捕获阶段。当您切换到 jQuery 版本时:
我认为发生的情况是第三个参数只是覆盖您的事件处理程序函数,并导致事件处理程序除了
返回 false;
之外不执行任何操作显然这不是应该发生的事情。来自jQuery 文档(添加了重点):
删除
false
参数,jQuery 版本也将正常工作:请注意,
alert
应该 显示,因此addEventListener 方法工作正常。请参阅@Pointy 的回答了解原因。
Look at the
addEventListener
version:That works fine because the third argument is
useCapture
, which specifies whether or not the capture phase of the event should be used.When you switch to the jQuery version:
I think what is happening is the third argument simply overrides your event handler function and causes the event handler to do nothing but
return false;
which is clearly not what is meant to happen.From the jQuery docs (emphasis added):
Remove the
false
argument and the jQuery version will work correctly too:Note that the
alert
should show up, so theaddEventListener
approach is working correctly. See @Pointy's answer for why that is.