在柔性目标和currentTarget当前的区别是什么? [英] What is the difference between target and currenttarget in flex?

查看:195
本文介绍了在柔性目标和currentTarget当前的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何一个可以请告诉我的目标和currentTarget当前的区别在柔性?

Can any one please tell me the difference between target and currenttarget in flex?

推荐答案

当然,我已经受够了这个有些麻烦了。该 currentTarget当前属性是IEventListener你注册的事件处理程序。该目标是一个分派你当前正在处理该事件。因此, currentTarget当前的变化,目标不会。

Sure, I've had some trouble with this too. The currentTarget property is the IEventListener you registered the event handler for. The target is the one that dispatched the event that you are currently handling. So the currentTarget changes, the target doesn't.

看看下面的例子:

示例应用程序

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="addListeners()">

    <mx:Script>
        <![CDATA[

            protected function addListeners():void
            {
                greatGrandParent.addEventListener(Event.COMPLETE, completeHandler);
                grandParent.addEventListener(Event.COMPLETE, completeHandler);
                aParent.addEventListener(Event.COMPLETE, completeHandler);
                child.addEventListener(Event.COMPLETE, completeHandler);
                // dispatch event that "bubbles", second param is "true"
                // dispatched from child
                child.dispatchEvent(new Event(Event.COMPLETE, true));
            }

            protected function completeHandler(event:Event):void
            {
                trace("target: ", event.target + ", currentTarget: ", event.currentTarget);
            }

        ]]>
    </mx:Script>

    <mx:Panel id="greatGrandParent">
        <mx:Panel id="grandParent">
            <mx:Panel id="aParent">
                <mx:Button id="child"/>
            </mx:Panel>
        </mx:Panel>
    </mx:Panel>

</mx:Application>

输出

target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent.grandParent.aParent.child
target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent.grandParent.aParent
target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent.grandParent
target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent

这是一个简单的显示对象的树,当应用程序已经准备好我:

It's a simple tree of display objects, and when the app is ready I:

  1. 添加监听器树中的每个组件上同样的事件。
  2. 在调度的任意事件(只是为了演示)。我选择了引发Event.COMPLETE

既然一切都已经注册了事件处理程序为同一事件,因为我已经设置为真(新的事件(类型,气泡)),在树中任何东西,从小孩到greatGrandParent和超越,已注册的事件处理程序引发Event.COMPLETE ,将运行方式:在completeHandler 。活动行程环比上涨然后回落。该目标是一个调度的事件,因此自孩子出动,它应该是恒定的。该 currentTarget当前是什么样的变化。

Since everything has registered an eventHandler for that same event, and since I have set bubbles to true (new Event(type, bubbles)), anything in the tree, from child to greatGrandParent and beyond, that has registered an event handler for Event.COMPLETE, will run that method: completeHandler. Events travel up the chain then back down. The target is the one that dispatched the event, so since child dispatched it, it should be constant. The currentTarget is what changes.

这意味着,假设你想,当你滚过Flex中一个DataGrid来检查,你想,当你翻转一个复选框里的DataGrid中的itemRenderer的一个知道的。一种方法是在对addEventListener每个itemRenderer的复选框 MouseEvent.ROLL_OVER 。另一种方法是在对addEventListener到DataGrid本身 MouseEvent.ROLL_OVER ,并检查什么的目标的是事件:

This means that, say you want to check when you are rolling over a DataGrid in Flex, you want to know when you roll over a Checkbox inside one of the itemRenderers in the DataGrid. One way is to addEventListener on every itemRenderer's checkbox for MouseEvent.ROLL_OVER. Another way is to addEventListener to the DataGrid itself for MouseEvent.ROLL_OVER, and check what the target is on the event:

protected function dataGrid_rollOverHandler(event:MouseEvent):void
{
    // event.currentTarget is DataGrid
    if (event.target is CheckBox)
        trace("rolled over checkbox!");
}

这就是我经常使用将event.target

希望帮助, 兰斯

这篇关于在柔性目标和currentTarget当前的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆