event.stopPropagation() 和event.stopImmediatePropagation()

转载:http://blog.sina.com.cn/s/blog_56ac8aeb0100qq7x.html


stopPropagation

() 方法  

public function stopPropagation():void
语言版本:  ActionScript 3.0
运行时版本:  AIR 1.0 Flash Player 9

防止对事件流中当前节点的后续节点中的所有事件侦听器进行处理。此方法不会影响当前节点 (currentTarget) 中的任何事件侦听器。相比之下,stopImmediatePropagation() 方法可以防止对当前节点中和后续节点中的事件侦听器进行处理。对此方法的其他调用没有任何效果。可以在事件流的任何阶段中调用此方法。

注意:此方法不会取消与此事件相关联的行为;有关此功能的信息,请参阅 preventDefault()

该事件流是为 SWF 内容中的显示对象定义的,它使用 ActionScript 3.0 显示对象模型。


stopImmediatePropagation

() 方法  
public function stopImmediatePropagation():void
语言版本:  ActionScript 3.0
运行时版本:  AIR 1.0 Flash Player 9

防止对事件流中当前节点中和所有后续节点中的事件侦听器进行处理。此方法会立即生效,并且会影响当前节点中的事件侦听器。相比之下,在当前节点中的所有事件侦听器都完成处理之前,stopPropagation() 方法不会生效。

注意:此方法不会取消与此事件相关联的行为;有关此功能的信息,请参阅 preventDefault()

该事件流是为 SWF 内容中的显示对象定义的,它使用 ActionScript 3.0 显示对象模型。


package
{
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    public class StopPropogation extends Sprite
    {
        private var msgTxt:TextField;
        public var msg_txt:TextField;
        private var father1:Sprite;
        private var father2:Sprite;
        private var child1:Sprite;
        private var child2:Sprite;
        public function StopPropogation()
        {
            this.father1 = new Sprite();
            this.father2 = new Sprite();
            this.child1 = new Sprite();
            this.child2 = new Sprite();
            this.msgTxt = this.getChildByName("msg_txt") as TextField;
            this.father1.graphics.beginFill(6671615);
            this.father1.graphics.drawRect(0, 0, 180, 140);
            this.father1.graphics.endFill();
            this.father1.x = 0;
            this.father1.y = 0;
            addChild(this.father1);
            this.father2.graphics.beginFill(5854273);
            this.father2.graphics.drawRect(0, 0, 180, 140);
            this.father2.graphics.endFill();
            this.father2.x = this.father1.x + this.father1.width + 5;
            this.father2.y = this.father1.y;
            addChild(this.father2);
            this.child1.graphics.beginFill(5854273);
            this.child1.graphics.drawRect(0, 0, 80, 40);
            this.child1.graphics.endFill();
            this.child1.x = 20;
            this.child1.y = 20;
            this.father1.addChild(this.child1);
            this.child2.graphics.beginFill(6671615);
            this.child2.graphics.drawRect(0, 0, 80, 40);
            this.child2.graphics.endFill();
            this.child2.x = 20;
            this.child2.y = 20;
            this.father2.addChild(this.child2);
            msgTxt = new TextField();
            msgTxt.x = 100;
            msgTxt.y = 200;
            this.addChild(msgTxt);
            this.father1.addEventListener(MouseEvent.CLICK, this.fatherClickHandler);
            this.father2.addEventListener(MouseEvent.CLICK, this.fatherClickHandler);
            this.child1.addEventListener(MouseEvent.CLICK, this.child1ClickHandler);
            this.child2.addEventListener(MouseEvent.CLICK, this.child2ClickHandler);
            this.child2.addEventListener(MouseEvent.CLICK,this.child3ClickHandler);//次优先级监听
            return;
        }// end function
        private function fatherClickHandler(event:MouseEvent) : void
        {
            trace("冒了。");
            this.msgTxt.appendText("冒了。\n");
            return;
        }// end function
        private function child1ClickHandler(event:MouseEvent) : void
        {
            trace("冒泡了嗎?");
            this.msgTxt.appendText("冒泡了嗎?\n");
            return;
        }// end function
        private function child2ClickHandler(event:MouseEvent) : void
        {
//            event.stopPropagation();//阻止父对象监听
            event.stopImmediatePropagation();//不但阻止了父对象的监听还将次监听级别的child3ClickHandler也被阻止了。
            trace("冒泡了嗎?");
            this.msgTxt.appendText("冒泡了嗎?\n");
            return;
        }// end function
                
        private function child3ClickHandler(event:MouseEvent) : void
        {
            event.stopPropagation();//阻止父对象监听
            trace("3冒泡了嗎?");
            this.msgTxt.appendText("3冒泡了嗎?\n");
            return;
        }
    }
}

你可能感兴趣的:(event.stopPropagation() 和event.stopImmediatePropagation())