flex中的target和currenttarget有什么区别? [英] What is the difference between target and currenttarget in flex?

查看:18
本文介绍了flex中的target和currenttarget有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我flex中target和currenttarget的区别?

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

推荐答案

当然,我也遇到了一些麻烦.currentTarget 属性是您为其注册事件处理程序的 IEventListener.target 是调度您当前正在处理的事件的目标.所以 currentTarget 改变了,target 没有改变.

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.

因为所有东西都为同一个事件注册了一个事件处理程序,并且因为我已经将 bubbles 设置为 true (new Event(type, bubbles)),所以树中的任何东西,从 child 到 greatGrandParent 及其他,为 Event.COMPLETE 注册了事件处理程序,将运行该方法:completeHandler.事件沿链向上传播,然后返回.target 是调度事件的那个,所以由于 child 调度了它,它应该是常量.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 中的一个 Checkbox.一种方法是在每个 itemRenderer 的 MouseEvent.ROLL_OVER 复选框上添加EventListener.另一种方法是为MouseEvent.ROLL_OVER 将EventListener 添加到DataGrid 本身,并检查事件上的target 是什么:

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的方式.

That's how I often use event.target.

希望对您有所帮助,兰斯

Hope that helps, Lance

这篇关于flex中的target和currenttarget有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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