flex target currentTarget

About the target and currentTarget properties
Every Event object has a target and a currentTarget property that help you to keep track of where it is in the process of propagation. The target property refers to the dispatcher of the event. The currentTarget property refers to the current node that is being examined for event listeners. When you handle a mouse event such asMouseEvent.CLICKby writing a listener on some component, theevent.target
property does not necessarily refer to that component; it is often a subcomponent, such as the Button control’s UITextField, that defines the label.When Flash Player or Adobe® AIR™ dispatches an event, it dispatches the event from the frontmost object under the mouse. Because children are in front of parents, that means the player or AIR might dispatch the event from an internal subcomponent, such as the UITextField of a Button.Theevent.targetproperty is set to the object that dispatched the event (in this case, UITextField), not the object that is being listened to (in most cases, you have a Button control listen for aclick
event).MouseEvent events bubble up the parent chain, and can be handled on any ancestor. As the event bubbles, the value of theevent.target
property stays the same (UITextField), but the value of theevent.currentTargetproperty is set at each level to be the ancestor that is handling the event. Eventually, the currentTarget will be Button, at which time the Button control’s event listener will handle the event. For this reason, you should use the event.currentTarget property rather than the event.target property; for example: <mx:Button label="OK" click="trace(event.currentTarget.label)"/> In this case, in the Button event’s click event listener, the event.currentTarget property always refers to the Button, while event.target might be either the Button or its UITextField, depending on where on the Button control the user clicked.
(每个事件对象都有target和currentTarget属性,帮助你追踪传播过程。target属性指的是事件的分配者(事件的根源)。currentTarget属性指的是被事件监听器检测的当前节点。
当你处理一个鼠标事件,例如注册在某些组件上的MouseEvent.CLICK事件,event.target的属性没必要引用这个组件,而是这个组件的子组件,例如用来定义标签(label)的Button的UITextField控件。
当Flash Player或者Adobe AIR 派发一个事件,它由鼠标下(可能是click、mouseOver等)的最前端对象派发。因为子类在父类的前面,这就意味着Flash Player或者AIR可能从内部子部件,例如Button的UITextField来派发事件。
event.target属性被设成派发事件的对象(在这里是UITextField),而不是被监听的对象(在多数情况,你会有一个监听click事件的Button空间)。
MouseEvent 事件往上冒泡到父链,可以被任何父类处理。随着事件冒泡,event.target的属性值不变(UITextFileld),但是event.currentTarget的属性被设成不同层级处理事件的父类。最后,currentTarget变成Button,这时Button控件的事件监听器将处理事件。因为这个原因,你应该用event.currentTarget属性而不是event.target属性,举例:
<mx:Button label="OK" click="trace(event.currentTarget.label)"/>
在这种情况,在Button事件监听器里,event.currentTarget属性总是指向Button,然而event.target可能或者是Button或者是它的UITextFiled,这取决于用户在Button控件上点击的位置。)

你可能感兴趣的:(Flex,Flash,Adobe)