flex target 、 currentTarget

 

During every event dispatch, the Event object passed to every event listener defines a

currentTarget variable that provides a reference to the object with which the event

listener registered. The following general event-listener code demonstrates; it outputs

the String value of the object with which someListener( ) registered:

public function someListener (e:SomeEvent):void {

// Access the object with which this event listener registered

trace(e.currentTarget);

}

For events targeted at nondisplay objects, the value of the Event class’s instance variable

currentTarget is always equal to target (because listeners always register with

the event target). For example, returning once again to the FileLoader class from

Example 12-1, if we check the value of both e.currentTarget and e.target within

completeListener( ), we find that those two variables refer to the same object:

package {

import flash.display.*;

import flash.net.*;

import flash.events.*;

public class FileLoader extends Sprite {

public function FileLoader ( ) {

var urlLoader:URLLoader = new URLLoader( );

urlLoader.addEventListener(Event.COMPLETE, completeListener);

urlLoader.load(new URLRequest("someFile.txt"));

}

private function completeListener (e:Event):void {

trace(e.currentTarget == e.target); // Displays: true

}

}

}

However, as we’ll learn in Chapter 21, for events targeted at display objects in a display

hierarchy, listeners can register both with the event target and with the event

target’s display ancestors. For event listeners registered with an event target’s display

ancestor, currentTarget refers to that display ancestor, while target refers to the

event target object.

For example, suppose a Sprite object that contains a TextField object registers a

MouseEvent.CLICK event listener, clickListener( ). When the user clicks the text field, a

MouseEvent.CLICK event is dispatched, and clickListener( ) is triggered. Within

clickListener( ), currentTarget refers to the Sprite object, while target refers to the

TextField object.

 

stone :

 

真正的事件dispatch者是event.target,监听事件(addEventListner)的对象是event.currentTarget,Flex skd中有言曰:

"Event objects also have target properties that reference the actual object which dispatched the event. In some cases, the target may not be the object for which you have registered a listener. This can occur when the object for which you have registered a listener contains a child component that also dispatches the same event (and
the event bubbles). If you want to ensure that you are getting a reference to the object for which the listener is registered to listen for the event, use the currentTarget property"

例:如为容器mc1创建了一个同类型(或者说拥有相同事件)的子容器mc2,再为mc1注册click事件监听器,当单击子容器mc2时,则event.target指事件dispatch者mc2,而event.currentTarget指当前的事件处理者mc1,因此在使用时如果是要获取被注册事件监听器的对象(一般都是如此)则用event.currentTarget ,currentTarget属性应具备两条件,一是它注册了侦听器,二是正在处理事件。

http://www.cnblogs.com/sharplife/archive/2007/09/15/893873.html  said

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值