【问题标题】:Selecting MXML siblings with actionscript, like javascript?使用动作脚本选择 MXML 兄弟姐妹,如 javascript?
【发布时间】:2010-06-17 01:40:20
【问题描述】:

我正在尝试获取类似于在 javascript 中选择兄弟姐妹的方式的 mxml 标记的兄弟姐妹。这在 Actionscript 中可行吗?

例如,当我单击 ID 为 textarea1TextArea 时,我需要它告诉我兄弟的 ID 为 rect1,以便我可以对其进行进一步处理。

<s:Group>
     <s:TextArea id="textarea1" click="getSibling(event)" />
     <s:Rect id="rect1" />
</s:Group>

【问题讨论】:

    标签: apache-flex actionscript-3 adobe mxml


    【解决方案1】:

    假设 Group、TextArea 和 Rect 是 UIComponents,我认为这应该可行:

        private function getSibling(e:Event):void {
            var parent:UIComponent = e.currentTarget.parent;
    
            if(parent) {
                var len:int = parent.numChildren;
                var child:UIComponent;
                for(var i:int = 0; i < len; i++) {
                    child = parent.getChildAt(i) as UIComponent;
                    if(child && child != e.currentTarget) {
                        trace(child.id);
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      我最初的想法是访问父级,然后检索其中的子级列表。

      function getSibling(e:Event):void { 
         //get an array of children from the parent.
         var children:Array = e.target.parent.getChildren();  
      
         //process children as you wish... 
      }
      

      这是关于Javascript here 的讨论。

      希望这会有所帮助。

      尼克

      nickgs.com

      【讨论】:

        【解决方案3】:

        据我所知,没有办法做到这一点。但是,textarea1 和 rect1 都是 Group 的子级。如果你给组一个 ID,你应该能够遍历所有的孩子来找到 TextArea 的所有兄弟姐妹。

        在 Flex 3 中,您将使用 for 循环、numChildren 和 getChildAt。我怀疑在 Flex 4 中会类似。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-05
          相关资源
          最近更新 更多