MouseEvent有target和currentTarget两个属性。这两个属性有区别,target返回的是最先接收到点击的交互物体,而currentTarget返回的是你addEventListener注册的这个对象。
target 属性
事件目标。 此属性包含目标节点。 例如,如果用户单击“确定”按钮,则目标节点就是包含该按钮的显示列表节点。
currentTarget 属性
当前正在使用某个事件侦听器处理 Event 对象的对象。 例如,如果用户单击“确定”按钮,则当前目标可以是包含该按钮的节点,也可以是它的已为该事件注册了事件侦听器的始祖之一。
例子:
<?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="955" minHeight="600"
width="1000" height="600"
creationComplete="init();">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private function init():void
{
groupTest.addEventListener(MouseEvent.CLICK , showClickHandler);
}
private function showClickHandler(evt:MouseEvent):void
{
trace(evt.target + " : " + evt.currentTarget);
}
]]>
</fx:Script>
<s:Group id="groupTest">
<s:Button id="btn" label="press me" width="100" height="50" x="127" y="137"/>
</s:Group>
</s:Application>
点击按钮后trace出来的对象是:
MM0.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.groupTest.btn : MM0.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.groupTest