Need help avoid HIERARCHY_REQUEST_ERR: DOM Exception 3 error
I'm using javascript with backbone and adobe air. In my view model, I have a parse function where it saves the response( Dom object ), later on, when user hits saves, I want to add a couple of nodes to that saved response but I'm getting Dom exception 3, any help? Here开发者_开发问答 are some code for reference.
parse: function(resp, xhr){
this.treeDoc = xhr.responseXML
//parse routine
}
Somewhere down the road, when user wants to save
var xmlTree = createMyXML(); //creates a sub xml tree
this.treeDoc.appendChild(xmlTree ); //DOM Error 3 thrown
Any advice?
You are likely to have to call adoptNode first:
this.treeDoc.adoptNode(newElements);
Then append to a child node like:
this.treeDoc.documentElement.firstChild.appendChild(newElements);
Additionally if newElements
is an array of new elements(like the name suggest) you will need to iterate through it.
See https://developer.mozilla.org/en/DOM/document for more info.
An answer to your comment(it was to large to be a comment it self):
Even if I didn't reproduce the error I think I know why you are getting it. Error 8 is the code for NOT_FOUND_ERR. Guess you save the value of childNodes.length
and iterate from 0(zero) to this value, what happens here is that the childNodes
array is updated every time a child is added or removed, and then in some point in your loop i
is actually greater than childNodes.length
(but not greater that the saved value) causing childNodes[i]
to return undefined
. And because undefined
is not a child of the referenced node the NOT_FOUND_ERR (code 8) is throw. To avoid this with minor changes to the code just iterates for the initial value of childNodes.length-1
to 0(zero), or just do:
while(node.childNodes.length > 0) node.removeChild(node.childNodes[0])
精彩评论