关于停止传播事件的实现非常简单,使用了stopPropagation和stopImmediatePropagation方法。
和JS作用基本相同:
topPropagation()方法阻止事件对象移到到另一个节点上,但是允许当前节点的其他事件监听函数执行,而stopImmediatePropagation()方法不仅阻止事件从当前节点移动到另一个节点上,它还不允许当前节点的其他事件监听函数执行。
以实现topPropagation方法为例
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init()" xmlns:net="net.*"> <fx:Script> <![CDATA[ import events.ContentEvent; import mx.collections.ArrayCollection; import mx.controls.Alert; import net.DataLoader; protected function init():void { button.addEventListener(MouseEvent.CLICK,onClick); box.addEventListener(MouseEvent.CLICK,onParentClick); } protected function onClick(event:Event) { Alert.show("As event.Calling Stop","First Event"); event.stopPropagation(); } protected function onParentClick(event:Event) { Alert.show("ParentCllick","Never Event"); } protected function onButtonClick(event:Event) { Alert.show("Start","button Event"); } ]]> </fx:Script> <fx:Declarations> </fx:Declarations> <fx:Metadata> </fx:Metadata> <s:Panel verticalCenter="0" horizontalCenter="0" title="Mix" width="450" height="300"> <s:HGroup id="box" width="100%"> <s:Button id="button" label="First Event" click="onButtonClick(event)"/> </s:HGroup> </s:Panel> </s:Application>