加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex_停止事件传播说明示例;

发布时间:2020-12-15 05:10:22 所属栏目:百科 来源:网络整理
导读:?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" ?? ??? ??? ??? minWidth="1024"
<?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" ?? ??? ??? ??? minWidth="1024" minHeight="768" pageTitle="TheStudioOfCenyebao" ?? ??? ??? ??? applicationComplete="initFn()"> ?? ? ?? ?<!-- Demo_如何停止事件传播 ?? ??? ?猜想:事件流_事件触发顺序 ?? ??? ??? ?首先被触发的是按钮里边添加的监听器(在MXML中添加),接着是通过addEventListener()添加的监听器, ?? ??? ?接着事件就冒泡到按钮父组件HGroup,触发父组件里边的监听器。 ?? ??? ??? ?不过使用event.stopPropagation()或者event.stopImmediatePropagation()都会阻止事件从按钮冒泡 ?? ??? ?到其父组件,两者的区别就是stopPropagation()函数会在给定组件(按钮)上的所有其他事件监听器完成对这个 ?? ??? ?事件(比如MouseEvent)的响应之后停止传播,而stopImmediatePropagation则是直接停止此事件传播。 ?? ?--> ?? ? ?? ?<fx:Script> ?? ??? ?<![CDATA[ ?? ??? ??? ?import mx.controls.Alert; ?? ??? ??? ? ?? ??? ??? ?protected function initFn():void{ ?? ??? ??? ??? ?// 添加事件监听器; ?? ??? ??? ??? ?button.addEventListener(MouseEvent.CLICK,onClick); ?? ??? ??? ??? ?box.addEventListener(MouseEvent.CLICK,onParentClick); ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ??? ?protected function button_clickHandler(event:MouseEvent):void ?? ??? ??? ?{ ?? ??? ??? ??? ?Alert.show("MXML click event.","MXML Event"); ?? ??? ??? ??? ?trace("第一个被触发 Event"); ?? ??? ??? ??? ?//event.stopPropagation();?? ?// 事件停止传播--但是第二个监听器会触发; ?? ??? ??? ??? ?event.stopImmediatePropagation();?? ?// 停止传播--后面所有监听器都不会触发; ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ??? ?protected function onClick(event:Event):void{ ?? ??? ??? ??? ?Alert.show("AS event.Calling stopPropagation","AS Event"); ?? ??? ??? ??? ?trace("第二个被触发 Event"); ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ??? ?protected function onParentClick(event:Event):void{ ?? ??? ??? ??? ?// 事件永远不会传播(冒泡)到父组件,不管使用event.stopPropagation()或者event.stopImmediatePropagation()。 ?? ??? ??? ??? ?Alert.show("You should never see this alert.","Parent Event"); ?? ??? ??? ??? ?trace("第三个被触发 Event"); ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ?]]> ?? ?</fx:Script> ?? ?<fx:Declarations> ?? ??? ?<!-- 非可视元素 --> ?? ?</fx:Declarations> ?? ? ?? ?<!--view--> ?? ?<s:HGroup id="box" horizontalCenter="0" verticalCenter="0"> ?? ??? ?<s:Button id="button" label="FireEvent" click="button_clickHandler(event)"/> ?? ?</s:HGroup> ?? ? </s:Application>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读