Event Bubbling, and Stop Propagation

你离开我真会死。 提交于 2019-12-17 20:08:32

问题


What is the difference between event.bubbles to false for any event, and setting event.stopPropagation() or stopImmediatePropagation() while handling event?

I'm using Flex4 with AS3.


回答1:


Setting bubbles to false means the event does not bubble up the display list at all.

stopPropagation() and stopImmediatePropagation() make the current event listener the last to process an event.

The difference between stopPropagation() and stopImmediatePropagation() is that stopImmediatePropagation() will not only prevent the event from moving to the next node, but it will also prevent any other listeners on that node from capturing their events.




回答2:


Information found at this article - Introduction to event handling in ActionScript 3.0 is more demonstrative and easy to understand. It will enhance the above accepted answer by @Jason Sturges.

Event bubbling and event capturing are two faces of events. If you make the event.bubbles to false that means the event is marked as non-bubbling event.

bubbles: Indicates whether or not the event is an event that bubbles (and captures). This does not mean that the event went through or is going through a capture or bubbles phase, but rather it is a kind of event that can.

Below image (from the above article) shows how the event goes through the process.

The difference of the stopPropagation() and stopImmediatePropagation() will be more clear in following images.

StopPropagation :

StopImmidiatePropagation :




回答3:


Look at the example:

object.addEventListener( MouseEvent.CLICK, functionOne );

object.addEventListener( MouseEvent.CLICK, functionTwo );

If functionOne contains event.stopPropagation(), functionTwo will be called as well. If it contains event.stopImmediatePropagation(), functionTwo will be ignored.



来源:https://stackoverflow.com/questions/7802601/event-bubbling-and-stop-propagation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!