getElementsByName似乎不起作用

时间:2012-10-25 13:15:48

标签: actionscript-3 flex getelementbyid

我正在使用Flex 4.6并拥有AIR应用程序

我有一个面板上有许多标签。我需要从另一个类访问这些标签。 我不想传递所有标签对象。所以我考虑过传递面板容器,然后通过getElementByName()方法访问标签。

这是我的标记:

<s:VGroup includeIn="SLA_textView" width="100%" height="100%"
paddingBottom="10" paddingRight="10">
<s:Panel id="SlaTextViewPanel" width="100%" height="100%"
 title="test View">
   <s:Label id="lbTotalBooks" name="test" x="82" y="62" />
</s:Panel>
</s:VGroup>

动作脚本代码如下所示:

getLabels(Container:Panel){
  var _Container:Panel = Container;

//var tempLabel:Label =  _Container.getChildByName("test") as Label;

var n:int = _Container.numChildren;
  for (var i:int = 0; i < n; i++) {
    var c:DisplayObject = _Container.getChildAt(i); 
    trace(c.name);
}

var tempLabel:Label =  _Container.getChildByName("test") as Label;

为什么tempLabel总是null

所以我试着在Panel上遍历对象的名称,测试标签甚至都没有显示出来?所以我在某处出了点问题。

跟踪:

instance1333
PanelSkin1074

2 个答案:

答案 0 :(得分:0)

您何时何地拨打getLabels方法?标签可能已创建但尚未初始化。

答案 1 :(得分:0)

您可以使用以下内容:

var element:Group = "searchId" in this ? this["searchId"] as Group : null;
相关问题