appendChild with SVG brings a HIERARCHY_REQUEST_ERR (3) in IE9.0

你离开我真会死。 提交于 2019-12-24 12:12:07

问题


The following code works fine in chrome and Firefox, but breaks in IE 9.0

//select div where svg is to be inserted
var selSVG = document.getElementById('SVGMap');
//clear any SVG
while (selSVG.hasChildNodes()) {
selSVG.removeChild(selSVG.lastChild);
}
//add the new svg
selSVG.appendChild(loadXML("roomlayouts/"+SelectedRoomAddress));

with loadXML(...) returning an svg document from another folder and with the error on the last line of DOM Exception: HIERARCHY_REQUEST_ERR (3) line 300 character 2

Any ideas why it's not working in IE 9.0?

The implementation is here: http://chocolatezombies.com/classroom/classroom.html


回答1:


You need to import the element from the loaded document into the document of selSVG before you may append it. Per the specs, you would do:

var room = loadXML(...);
if (room.nodeType==9){         // You can't import entire documents, so
  room = room.documentElement; // get the root node of the document
}
room = selSVG.ownerDocument.importNode( room );
selSVG.appendChild( room );

However, IE9 does not properly implement importNode. I have provided a workaround method for this as part of my answer to this question: How do I dynamically insert an SVG image into HTML?



来源:https://stackoverflow.com/questions/8078948/appendchild-with-svg-brings-a-hierarchy-request-err-3-in-ie9-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!