---
title: "flex currenTarget与 target 的区别"
author: "kael____"
date: "2012-03-24T14:04:00Z"
tags: ["Flex", "event", "flex currenTarget", "flex target"]
categories: ["Web前端"]
slug: "flex-currentarget-target"
draft: false
lastmod: "2012-07-19T17:25:22Z"
id: 1597466
url: "https://dota747.iteye.com/blog/1597466"
---

开始学习Flex，会在别人的代码中经常看到target和currentTarget这两个属性，一般是event.target、event.currentTarget，那他们两个有什么区别呢？

简单说，target是事件的调用对象(eventdispatcher)，currentTarget是事件的处理对象(eventprocessor)

下面贴一个实例：

```
<?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"
			   creationComplete="init()"
			   name="application"
			   >
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<s:layout>
		<s:HorizontalLayout/>
	</s:layout>
	<s:Button id="btn01" name="btn01" label="button1"/>
	<s:Button id="btn02" name="btn02" label="button2" click="clickHandle(event)"/>
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private function init():void {
				addEventListener(MouseEvent.CLICK,clickHandle);
			}
			private function clickHandle(event:MouseEvent):void {
				Alert.show("target: "+(event.target as DisplayObject).name+" \ncurrentTarget: "+(event.currentTarget as DisplayObject).name);
			}
		]]>
	</fx:Script>
</s:Application>
```

说明：

1\. 点击btn，btn是事件发送者，因此，target是btn；  
2\. app注册了事件监听器，用于监听MouseEvnet.Click事件，是事件处理者，因此currentTarget是app  
3\. btn2自己注册了click事件，那么btn2既是事件发送者，又是处理者，因此，target和currentTarget都是btn2，这种情况下，两者是一致的  
4\. 同时注意，由于app注册了鼠标点击事件，在点击btn2的时候，首先响应的是app的处理器，然后才是btn2的处理器

通过上面的分析，应该比较清楚的区分什么时候用target，什么时候用currentTarget了，在实际编程中实践几次，就不会感到迷惑了。