jQuery.on();与 JavaScript .addEventListener();

发布于 2024-12-28 16:37:26 字数 702 浏览 0 评论 0原文

有人可以解释一下为什么事件处理程序的执行顺序会根据它们的附加方式而有所不同吗?在下面的示例中,我使用 .on().addEventListener() 方法来处理不同 DOM 元素上的特定事件。

jsfiddlehttp://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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2025-01-04 16:37:26

当您在文档级别使用 .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 to event.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.

最丧也最甜 2025-01-04 16:37:26

看一下 addEventListener 版本:

document.getElementById('outer').addEventListener('mouseup', function (event) {
    alert('This alert should not show up!');
}, false);

它工作得很好,因为第三个参数是 useCapture,它指定是否应该使用事件的捕获阶段。

当您切换到 jQuery 版本时:

$('#outer').on('mouseup', function (event) {
    alert('This alert should not show up!');
}, false);

我认为发生的情况是第三个参数只是覆盖您的事件处理程序函数,并导致事件处理程序除了返回 false; 之外不执行任何操作显然这不是应该发生的事情。

来自jQuery 文档(添加了重点):

事件触发时执行的函数。 值为 false 是
也允许作为简单返回的函数的简写
错误

删除 false 参数,jQuery 版本也将正常工作:

$('#outer').on('mouseup', function (event) {
    alert('This alert should not show up!');
});

请注意,alert 应该 显示,因此 addEventListener 方法工作正常。请参阅@Pointy 的回答了解原因。

Look at the addEventListener version:

document.getElementById('outer').addEventListener('mouseup', function (event) {
    alert('This alert should not show up!');
}, false);

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:

$('#outer').on('mouseup', function (event) {
    alert('This alert should not show up!');
}, false);

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):

A function to execute when the event is triggered. The value false is
also allowed as a shorthand for a function that simply does return
false
.

Remove the false argument and the jQuery version will work correctly too:

$('#outer').on('mouseup', function (event) {
    alert('This alert should not show up!');
});

Note that the alert should show up, so the addEventListener approach is working correctly. See @Pointy's answer for why that is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文