Import SVG node into another document in IE9

后端 未结 1 993
不知归路
不知归路 2020-12-20 02:56

After fetching an SVG document using XHR I need to append a portion of it from the responseXML document into the current document. Using this code works on Safa

相关标签:
1条回答
  • 2020-12-20 03:29

    Here's a workaround solution that manually 'imports' the node by using a custom function to recursively clone all portions of it instead of using importNode. This code is used on my example page to import one of the two shapes.

    var xhr = new XMLHttpRequest;
    xhr.open('get','stirling4.svg',true);
    xhr.onreadystatechange = function(){
      if (xhr.readyState != 4) return;
      var g = xhr.responseXML.getElementsByTagName('g')[2];
      var p = document.getElementsByTagName('path')[0];
      p.parentNode.insertBefore(cloneToDoc(g),p);
    };
    xhr.send();
    
    function cloneToDoc(node,doc){
      if (!doc) doc=document;
      var clone = doc.createElementNS(node.namespaceURI,node.nodeName);
      for (var i=0,len=node.attributes.length;i<len;++i){
        var a = node.attributes[i];
        clone.setAttributeNS(a.namespaceURI,a.nodeName,a.nodeValue);
      }
      for (var i=0,len=node.childNodes.length;i<len;++i){
        var c = node.childNodes[i];
        clone.insertBefore(
          c.nodeType==1 ? cloneToDoc(c,doc) : doc.createTextNode(c.nodeValue),
          null
        );
      }
      return clone;
    }
    
    0 讨论(0)
提交回复
热议问题