Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将SVG元素添加到带有appendChild的HTML元素中-在IE9中失败?_Javascript_Svg_Prototypejs - Fatal编程技术网

Javascript 将SVG元素添加到带有appendChild的HTML元素中-在IE9中失败?

Javascript 将SVG元素添加到带有appendChild的HTML元素中-在IE9中失败?,javascript,svg,prototypejs,Javascript,Svg,Prototypejs,我通过一个原型Ajax请求加载一个SVG文件,向其中添加一个类和一个id,然后将其放入一个div中。我的问题是将其添加到DOM中: onSuccess: function(response) { var svgElement = response.responseXML.documentElement; svgElement.setAttribute("id", "someId"); svgElement.setAttribute("class", "someClas

我通过一个原型Ajax请求加载一个SVG文件,向其中添加一个类和一个id,然后将其放入一个div中。我的问题是将其添加到DOM中:

onSuccess: function(response) { 

    var svgElement = response.responseXML.documentElement;
    svgElement.setAttribute("id", "someId");
    svgElement.setAttribute("class", "someClass");

    // At this point, everything is fine. I can verify that in IE9,
    // I have a valid svgElement and the id and class have been correctly set.

    var someDiv = $('someDiv');

    someDiv.appendChild(svgElement); // This fails in IE9, but works elsewhere!
    someDiv.insert(svgElement.xml); // This works in IE9, but fails elsewhere!

}
我只关心那些更好的浏览器——IE9是我在这里需要担心的最低版本

有什么事吗?我暂时切换插入方法,这取决于我是否在IE中,但我想弄清这一点并以正确的方式进行修复。

我通过使用responseText在所有情况下创建新文档来解决“”问题:

onSuccess: function(response) { 

    var svgDoc;

    if (window.DOMParser) {
      var domParser = new DOMParser();
      svgDoc = domParser.parseFromString(response.responseText, "text/xml");
    } else {
      svgDoc = new ActiveXObject("Microsoft.XMLDOM");
      svgDoc.async = false;
      svgDoc.loadXML(response.responseText);
    }

    var svgElement = svgDoc.documentElement;

}
如果你问我的话,这比导入文档(以及所有相关问题)更简单、更清晰