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

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

问题描述

我们正在开发一个JavaScript工具,其中包含较旧的代码,
,因此我们无法重新编写整个工具。



现在,菜单被添加的位置固定在底部,客户端将非常喜欢有一个切换按钮来打开和关闭菜单,
除了关闭需要自动发生,当用户开始在菜单的一边做事情,为例如,当用户回到页面,并在表单域中选择某些东西或点击时。



这可以在技术上与点击事件在正文上,触发任何点击,
但是在旧代码中有许多项目,其中处理了一个点击事件内部链接和返回false 已添加到点击功能,以便网站不要继续链接的 href 标签。



很清楚,这样的一般功能确实有效,但是当点击内部链接时,返回false停止支路($($)$($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ );
});

有没有办法我可以强制身体点击事件,
或是否有另一种方式可以让菜单消失,使用一些全球点击事件或类似的东西?



无需重写应用程序中的所有其他点击是在几年前创立的。
这将是一个怪物的任务,特别是因为我不知道如何重写它们,而不返回false,但仍然不让他们去他们的 href

解决方案

现代DOM实现中的事件有两个阶段,捕获冒泡。捕获阶段是第一阶段,从文档的 defaultView 流向事件目标,然后是从事件目标返回到$ $ c的冒泡阶段$ C>默认视图。有关详细信息,请参阅 http://www.w3.org/TR/DOM-级别3事件/#事件流程



要处理事件的捕获阶段,您需要设置<$ c的第三个参数$ c> addEventListener to true

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

令人遗憾的是,正如韦斯利所说,事件的捕获阶段无法可靠地处理,在旧的浏览器中。



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


  1. mousedown

  2. mouseup

  3. 点击

如果您确定没有处理程序取消 mouseup 事件,那么这是一种方式(可以说是一种更好的方式)去。另外需要注意的是,如果不是大部分(如果不是全部),UI菜单将在鼠标停止时消失。


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.

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.

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');
});

Is there a way I can force the body click event anyway, or is there another way I can let the menu dissappear, using some global click event or anything similar?

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.

解决方案

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.

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

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

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

  1. mousedown
  2. mouseup
  3. click

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天全站免登陆