【问题标题】:Flex event propagationFlex 事件传播
【发布时间】:2010-04-21 12:34:18
【问题描述】:

我试图捕捉一个事件,但我不能。 请看这里: http://www.pata.ro/FlexTest/ 蓝色矩形是父级,另外两个是子级。 如果我翻过一个孩子(红色或绿色),我仍然在蓝色孩子上(不会为蓝色孩子触发 RollOut)。我让绿色矩形有点透明,所以你可以看到它在红色矩形上方。 当我将光标放在红色光标上方的绿色光标上时,我得到了 BlueRollOver、GreenRollOver、RedRollOut。 我尝试做的是也获得红色的 RollOver,即使它在绿色的下方。就像父母捕获翻转一样,即使我超过了它的一个孩子。或相反亦然。 那么,如何将事件向下传播到鼠标悬停的元素下的元素?

谢谢


你有我的代码。 事件侦听器是在 MXML 中声明的,因此我为红色矩形重写了这些侦听器,以便添加 useCapture 参数。如果我将 useCapture 设置为 TRUE,那么无论我有鼠标在哪里,红色矩形都不会捕获任何事件。如果我将它设置为 false,它会像以前一样工作。那么,我该如何使用这个参数呢?

<?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"
               applicationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            private function init():void
            {
                gRed.addEventListener(MouseEvent.ROLL_OVER,RedRollOver,true);
                gRed.addEventListener(MouseEvent.ROLL_OUT,RedRollOut,true);
            }

            private function BlueRollOver(ev:Event):void
            {
                idBlue.text="RollOver";
            }
            private function BlueRollOut(ev:Event):void
            {
                idBlue.text="RollOut";
            }
            private function RedRollOver(ev:Event):void
            {
                idRed.text="RollOver";
            }
            private function RedRollOut(ev:Event):void
            {
                idRed.text="RollOut";
            }
            private function GreenRollOver(ev:Event):void
            {
                idGreen.text="RollOver";
            }
            private function GreenRollOut(ev:Event):void
            {
                idGreen.text="RollOut";
            }
        ]]>
    </fx:Script>
    <s:Group id="gBlue" x="114" y="94" width="404" height="301" rollOver="BlueRollOver(event)" rollOut="BlueRollOut(event)">
        <s:Rect width="100%" height="100%">
            <s:fill>
                <s:SolidColor color="#0000CC"/>
            </s:fill>
        </s:Rect>
        <s:Group id="gRed" x="140" y="101" width="230" height="114">
            <s:Rect width="100%" height="100%">
                <s:fill>
                    <s:SolidColor color="#EE0000"/>
                </s:fill>
            </s:Rect>
        </s:Group>
        <s:Group id="gGreen" x="39" y="20" width="200" height="200" rollOver="GreenRollOver(event)" rollOut="GreenRollOut(event)">
            <s:Rect width="100%" height="100%" alpha="0.6">
                <s:fill>
                    <s:SolidColor color="#00EE00"/>
                </s:fill>
            </s:Rect>
        </s:Group>
    </s:Group>
    <s:Label x="535" y="94" text="Blue" color="#0000CC" width="149" id="idBlue"/>
    <s:Label x="535" y="114" text="Red" color="#EE0000" width="173" id="idRed"/>
    <s:Label x="535" y="137" text="Green" color="#00EE00" width="173" id="idGreen"/>
</s:Application>

【问题讨论】:

    标签: apache-flex events


    【解决方案1】:

    在将事件侦听器添加到红色层时,尝试将 useCapture 参数设置为 true。

    【讨论】:

      【解决方案2】:

      这是因为绿色和红色矩形是兄弟,只有当有父/子层次结构时,事件才能冒泡。在红色矩形上使用 useCapture 标志。

      【讨论】:

      • 对不起。我已经阅读并测试了 useCapture,但它不起作用。它用于其他用途,而不是在兄弟姐妹之间传播事件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 2010-11-13
      • 2014-09-12
      • 2017-07-05
      • 2011-11-05
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多