target和currentTarget的区别

这两天 看程序 写程序 对Event的target和currentTarget的区别不是很了解 
想深究 于是从网上查了一些资料 
一般是event.target、event.currentTarget,那他们两个有什么区别呢?
简单说,target是事件的调用对象(event dispatcher),currentTarget是事件的处理对象(event processor)


1.target在事件流的目标阶段;currentTarget在事件流的捕获,目标及冒泡阶段。只有当事件流处在目标阶段的时候,两个的指向才是一样的,而当处于捕获和冒泡阶段的时候,target指向被单击的对象而currentTarget指向当前事件活动的对象(一般为父级)。
2.currentTarget属性具备两条件:注册侦听器;正在处理事件。
例子:

  1. mc1.addChild(mc2);
  2. mc1.addEventListener(MouseEvent.CLICK,doit1);
  3. function doit1(e){
  4. trace(e.target.name);//点击mc2,输出 mc2
  5. trace(e.currentTarget.name);//点击mc2,输出 mc1
  6. }
  7. mc2.addEventListener(MouseEvent.CLICK,doit2);
  8. function doit2(e){
  9. trace(e.target.name);//点击mc2,输出 mc2
  10. trace(e.currentTarget.name);//点击mc2,输出 mc2
  11. }
复制代码

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

真正的事件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"

转载于:https://www.cnblogs.com/JamesKing/archive/2012/10/10/2718465.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值