【问题标题】:appendChild with SVG brings a HIERARCHY_REQUEST_ERR (3) in IE9.0appendChild with SVG 在 IE9.0 中带来了 HIERARCHY_REQUEST_ERR (3)
【发布时间】:2011-12-26 02:37:23
【问题描述】:

以下代码在 chrome 和 Firefox 中运行良好,但在 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));

使用 loadXML(...) 从另一个文件夹返回 svg 文档,并且在 DOM 异常的最后一行出现错误:HIERARCHY_REQUEST_ERR (3) line 300 character 2

任何想法为什么它不能在 IE 9.0 中工作?

实现在这里:http://chocolatezombies.com/classroom/classroom.html

【问题讨论】:

  • loadXML 究竟返回了什么?
  • 它返回一个完整的 SVG 文档,例如:chocolatezombies.com/classroom/roomlayouts/S110.svg
  • @999 我应该说它返回 SVG 文件的文档元素。抱歉,只是更仔细地查看了我的代码。复制下面的函数 function loadXML(url) { var xmlhttp = new window.XMLHttpRequest(); xmlhttp.open("GET", url, false); //停止烦人的缓存! xmlhttp.setRequestHeader("缓存控制", "无缓存"); //正如上面所说的 xmlhttp.send(null);返回 xmlhttp.responseXML.documentElement; }

标签: javascript svg internet-explorer-9


【解决方案1】:

你需要 import 将加载文档中的元素添加到 selSVG 的文档中,然后才能附加它。根据规格,您会这样做:

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 );

但是,IE9 没有正确实现importNode。作为对此问题的回答的一部分,我为此提供了一种解决方法:How do I dynamically insert an SVG image into HTML?

【讨论】:

  • 嗨@Phrogz,我正在努力让它工作。我查看了您附加的示例代码,它在 IE9.0 中不断出现以下错误:Unable to get value of the property 'namespaceURI': object is null or undefined 在以下行 var clone = doc.createElementNS(node.namespaceURI,node.nodeName); 澄清:loadXML(...) 正在返回以下内容:xmlhttp.responseXML.documentElement;
  • 为了进一步澄清,我正在加载的 SVG 是在 inkscape 中创建的,并且具有所有适当的命名空间信息,即:xmlns="http://www.w3.org/2000/svg"
  • @pluke 如果您需要进一步的帮助,请制作一个显示此问题的测试用例并在线托管;否则,您将需要学习调试。 (提示:该错误意味着node 的值为空。您需要找出原因。)
  • 嗨@Phrogz,我发现了这个问题。 inkscape 输出的 SVG 比你的xmlns="http://www.w3.org/2000/svg" 有更多的命名空间信息 如:xmlns:svg="w3.org/2000/svg" xmlns="w3.org/2000/svg" xmlns:xlink="w3.org/1999/xlink"' 你的代码if (a.nodeName=="xmlns") continue; clone.setAttributeNS(a.namespaceURI,a.nodeName,a.nodeValue); 可以' t 处理 xmlns 的子集。很抱歉响应缓慢,我对 Win7 机器的访问权限有限。试着用你的例子加载这个:chocolatezombies.com/classroom/roomlayouts/S110.svg
  • @pluke 啊,没错。我已经修复了我的示例以改用if (/^xmlns\b/.test(a.nodeName)) continue;,并确认您的 S110.svg 文件能够通过该更改加载到 IE9 中。感谢您的报告。
猜你喜欢
  • 2023-04-07
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
相关资源
最近更新 更多