【问题标题】:What is the difference between target and currenttarget in flex?flex中的target和currenttarget有什么区别?
【发布时间】:2010-02-25 12:52:59
【问题描述】:

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

【问题讨论】:

    标签: actionscript-3 apache-flex actionscript event-handling event-delegation


    【解决方案1】:

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

    查看以下示例:

    示例应用

    <?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
    

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

    1. 为树中的每个组件上的同一事件添加侦听器。
    2. 调度任意事件(仅用于演示)。我选择了Event.COMPLETE

    由于所有东西都为同一个事件注册了一个事件处理程序,并且由于我已将bubbles 设置为 true (new Event(type, bubbles)),因此树中的任何东西,从 child 到 greatGrandParent 及以后,都为 @ 注册了一个事件处理程序987654330@,将运行该方法:completeHandler。事件沿着链条向上传播,然后再向下传播。 target 是发送事件的那个,所以既然child 发送了它,它应该是常量。 currentTarget 发生了变化。

    这意味着,假设您想在 Flex 中滚动 DataGrid 时进行检查,您想知道何时滚动 DataGrid 中的 itemRenderer 内的 Checkbox。一种方法是在每个 itemRenderer 的MouseEvent.ROLL_OVER 复选框上添加EventListener。另一种方法是为MouseEvent.ROLL_OVER 将EventListener 添加到DataGrid 本身,并检查事件中的target 是什么:

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

    这就是我经常使用event.target的方式。

    希望对您有所帮助, 兰斯

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      在问这样的问题之前,您应该先阅读以下网站上的教程:http://www.adobe.com/devnet/flex/videotraining/,了解 Flex 的介绍。第 1 天就涵盖了您的问题。

      【讨论】:

        猜你喜欢
        • 2016-06-12
        • 2020-02-14
        • 2011-10-28
        • 1970-01-01
        • 2021-11-01
        • 2012-02-08
        • 2019-02-01
        • 2012-05-26
        • 2023-03-20
        相关资源
        最近更新 更多