即使某个元素停止传播,我如何处理页面中任意位置的点击? [英] How do I handle a click anywhere in the page, even when a certain element stops the propagation?

查看:22
本文介绍了即使某个元素停止传播,我如何处理页面中任意位置的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个包含旧代码的 JavaScript 工具,所以我们不能重写整个工具.

We are working on a JavaScript tool that has older code in it, so we cannot re-write the whole tool.

现在,菜单被添加到固定在底部的位置,客户非常希望它有一个切换按钮来打开和关闭菜单,除了当用户开始在菜单外执行操作时需要自动关闭,例如,当用户返回页面并选择某些内容或单击表单字段时.

Now, a menu was added position fixed to the bottom and the client would very much like it to have a toggle button to open and close the menu, except closing needs to happen automatically when a user starts doing things out side of the menu, for example, when a user goes back into the page, and selects something or clicks on a form field.

这在技术上可以与 body 上的 click 事件一起使用,在任何点击时触发,然而,在旧代码中有许多项目,其中在内部链接上处理了点击事件,并且在点击功能中添加了return false,以便网站不继续链接的 return falsecode>href 标签.

This could technically work with a click event on the body, triggering on any click, however there are numerous items in the older code, where a click event was handled on an internal link, and return false was added to the click function, in order for the site not to continue to the link's href tag.

很明显,像这样的通用函数确实有效,但在点击返回 false 停止传播的内部链接时无效.

So clearly, a general function like this does work, but not when clicked on an internal link where the return false stops the propagation.

$('body').click(function(){
  console.log('clicked');
});

无论如何我可以强制执行正文单击事件,或者有没有其他方法可以让菜单消失,使用一些全局点击事件或类似的东西?

无需重写应用程序中多年前创建的所有其他点击.这将是一项艰巨的任务,尤其是因为我不知道如何重写它们,而不返回 false,但仍然不要让它们进入 href.

Without having to rewrite all other clicks in the application that were created years ago. That would be a monster task, especially since I have no clue how I would rewrite them, without the return false, but still don't let them go to their href.

推荐答案

现代 DOM 实现中的事件有两个阶段,捕获冒泡.捕获阶段是第一个阶段,从文档的 defaultView 流向事件目标,然后是冒泡阶段,从事件目标流回 defaultView.有关详细信息,请参阅 http://www.w3.org/TR/DOM-3 级事件/#event-flow.

Events in modern DOM implementations have two phases, capturing and bubbling. The capturing phase is the first phase, flowing from the defaultView of the document to the event target, followed by the bubbling phase, flowing from the event target back to the defaultView. For more information, see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow.

要处理事件的捕获阶段,您需要将 addEventListener 的第三个参数设置为 true:

To handle the capturing phase of an event, you need to set the third argument for addEventListener to true:

document.body.addEventListener('click', fn, true); 

遗憾的是,正如 Wesley 提到的,在旧浏览器中无法可靠地处理事件的捕获阶段,或者根本无法处理.

Sadly, as Wesley mentioned, the capturing phase of an event cannot be handled reliably, or at all, in older browsers.

一种可能的解决方案是处理 mouseup 事件,因为点击的事件顺序是:

One possible solution is to handle the mouseup event instead, since event order for clicks is:

  1. 鼠标按下
  2. 鼠标
  3. 点击

如果您可以确定没有处理程序取消 mouseup 事件,那么这是一种方法(并且可以说是更好的方法).另一件需要注意的事情是,许多(如果不是大多数)(如果不是全部)UI 菜单会在鼠标按下时消失.

If you can be sure you have no handlers cancelling the mouseup event, then this is one way (and, arguably, a better way) to go. Another thing to note is that many, if not most (if not all), UI menus disappear on mouse down.

这篇关于即使某个元素停止传播,我如何处理页面中任意位置的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆