在AS3中遇到insertChildBefore和insertChildAfter时遇到问题

ner*_*lly 3 xml actionscript-3

我有一个XML文档:

var xml:XML = new XML(<rootNode>            
                <head> 
                    <meta name="template" content="Default" />
                </head>
                <mainSection>
                    <itemList>
                        <item>
                            <video src={this.videoURL}  />
                            <img  src={this.src}></img>
                        </item>
                    </itemList>
                </mainSection>
            </rootNode>);
Run Code Online (Sandbox Code Playgroud)

我想做的是,当我有某些条件时,在itemList的开头插入另一个条件.

var newNode:XMLList = new XMLList("<item><video src=\"" + _videoSource + "\"></video></item>");
Run Code Online (Sandbox Code Playgroud)

我能够很好地生成和跟踪newNode,但每当我尝试使用insertChildBefore它添加它时,它总是返回undefined.

var contentNode:XML = new XML(xml.mainSection.itemList.item);
xml.insertChildBefore(contentNode ,newNode)
Run Code Online (Sandbox Code Playgroud)

contentNode一直跟踪很好,但尝试时总是失败insertChildBeforeinsertChildAfter.奇怪的是,如果我contentNode不那么具体(比如xml.mainSection),那么它会按预期工作.

谢谢你的帮助,这让我疯了!

Mat*_*ard 5

这里有两个问题(我现在测试了这段代码 - 它应该对你有用):

  1. 该变量xml不是item您要插入的节点的直接父级.你调用insertChildBeforexml的节点,但你contentNode是不是它的直接孩子.

  2. contentNode您尝试在之前插入的变量是您想要的节点的副本 ; 你不应该创建一个全新的XML对象.

试试这个:

var contentNode:XML = xml.mainSection.itemList.item[0];
var parentNode:XML = xml.mainSection.itemList[0];
parentNode.insertChildBefore( contentNode, newNode[0] );
Run Code Online (Sandbox Code Playgroud)