简体   繁体   English

Javascript中的事件传播

[英]Event propagation in Javascript

If I have an element (html) nested in another element and both of them have a click handler attached, clicking the inner element executes its click handler and then bubbles up to the parent and executes its click handler. 如果我有一个嵌套在另一个元素中的元素(html)并且它们都附加了一个单击处理程序,则单击内部元素执行其单击处理程序,然后冒泡到父级并执行其单击处理程序。 That's how I understand it. 这就是我理解它的方式。

Do events bubble up the DOM tree if there are no events attached that are the same and if so, is it worth putting a event.stopPropagation() at the end of every handler to stop this and speed things up? 如果没有附加的事件是相同的,事件会冒出DOM树吗?如果是这样的话,是否值得在每个处理程序的末尾放置一个event.stopPropagation()来阻止它并加快速度?

events almost always bubble up unless event.cancelBubble=true is set or event.stopPropagation() is used. 事件几乎总是冒泡,除非设置了event.cancelBubble = true或者使用了event.stopPropagation()。 You are only aware of it, though, when one of your event handlers gets tripped. 但是,当您的某个事件处理程序被触发时,您才会意识到这一点。

See http://en.wikipedia.org/wiki/DOM_events for a list of events which bubble. 请参阅http://en.wikipedia.org/wiki/DOM_events以获取冒泡的事件列表。 (Note: in the table of HTML events, cancelable refers to the effectiveness of event.preventDefault() or return false to cancel the default action, not bubbling) (注意:在HTML事件表中,cancelable指的是event.preventDefault()的有效性,或者返回false来取消默认操作,而不是冒泡)

Also see http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow , in particular 1.2.1 Basic Flow to understand the capture phase and bubbling phase of event propagation. 另请参阅http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow ,特别是1.2.1基本流程,以了解事件传播的捕获阶段和冒泡阶段。

EDIT 编辑

http://mark-story.com/posts/view/speed-up-javascript-event-handling-with-event-delegation-and-bubbling suggests there is a performance gain by stopping propagation but provides no data. http://mark-story.com/posts/view/speed-up-javascript-event-handling-with-event-delegation-and-bubbling表明存在通过停止传播性能增益,但不提供数据。

http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/a9af0aa4216a8046 suggests that browsers should be optimized for bubbling behaviour and says there should be no significant performance difference. http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/a9af0aa4216a8046建议浏览器应针对冒泡行为进行优化,并表示应该没有明显的性能差异。 Again no data. 再没有数据。

http://developer.yahoo.com/performance/rules.html#events provides a good technique for improving event-handling performance, but doesn't directly talk about stopPropagation performance. http://developer.yahoo.com/performance/rules.html#events提供了一种改进事件处理性能的好方法,但没有直接讨论stopPropagation性能。

Ultimately, you'd have to profile the difference to get a good idea of the benefits on your site. 最终,您必须分析差异,以便更好地了解您网站的好处。

I suppose this behavior is already well optimized by browsers, so you won't be able to catch significant performance boost when stopping propagations (except, perhaps, for really-really complex nested DOM structures). 我认为浏览器已经很好地优化了这种行为,因此在停止传播时你将无法获得显着的性能提升(除非真的非常复杂的嵌套DOM结构)。 If you are worried by performance and deal with lots of events, you may be interested in event delegation instead. 如果您担心性能并处理大量事件,您可能会对事件委派感兴趣。

Also, you should remember your code should stay readable and self-explainable. 此外,您应该记住您的代码应该保持可读性和自我解释。 stopPropagation() is a method used for certain purpose, so using it in every method could be really confusing. stopPropagation()是一种用于特定目的的方法,因此在每种方法中使用它可能会让人感到困惑。

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

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