访问外部SVG文件中定义的DOM对象

rod*_*gob 12 javascript svg dom

SVG标准允许使用和引用外部SVG文件.

我有一个文件circle.svg定义了一个id为"the_circle"的圆形对象.从主SVG文件中我可以使用SVG链接包含此圆圈并为其设置动画.

我也想通过javascript访问相同的圈子对象,我该怎么做?什么是javascript相当于xlink:href="url(#the_image)#the_circle"

使用document.getElementById('the_image')我只能访问SVGImageElement,但不能访问包含的SVG中定义的对象.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >

  <image 
    id="the_image"
    x="0" y="0"  width="100%" height="100%"
    xlink:href="circle.svg" />

  <animateTransform     
    xlink:href="url(#the_image)#the_circle"

    attributeName="transform" attributeType="XML"
    type="translate" 
    from="0" to="25"
    dur="1s" repeatCount="indefinite" 
    additive="replace" fill="freeze" />
</svg>
Run Code Online (Sandbox Code Playgroud)

jbe*_*rd4 14

看起来这样做的"正确"方法实际上是使用SVG"use"元素,而不是图像.原因是SVG use元素的DOM接口指定了一个属性"instanceRoot",它允许您获取与该use元素对应的"实例树"的根:http://www.w3.org/ TR/SVG/struct.html#InterfaceSVGUseElement

因此,您最终会得到一个类似于以下内容的解决方案:circle.svg:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="4in" height="4in" id="the_svg"
     viewBox="0 0 4 4" version="1.1"
     xmlns="http://www.w3.org/2000/svg">
    <circle r="1" fill="blue" stroke="none" id="the_circle"/>
</svg>
Run Code Online (Sandbox Code Playgroud)

使用circle.svg的svg根节点的文档:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" id="foo"
     version="1.1"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"> 

    <use xlink:href="circle.svg#the_svg"/>
</svg>
Run Code Online (Sandbox Code Playgroud)

不幸的是,虽然Firefox支持将use元素与外部文档一起使用,但Webkit目前存在一个不允许这样做的错误:https://bugs.webkit.org/show_bug.cgi?id = 12499

此外,Firefox似乎没有为use元素实现instanceRoot属性.

因此,似乎您可能需要解决当前SVG实现的限制.我建议这样做的方法是使用XMLHttpRequest下载您要链接的文档,并将下载文档的DOM导入到主机文档的DOM中.以下代码实现了这一点,适用于Firefox,Opera和Chromium:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" id="foo"
     version="1.1"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"> 

    <script>
    function fetchXML  (url, callback) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.onreadystatechange = function (evt) {
        //Do not explicitly handle errors, those should be
        //visible via console output in the browser.
        if (xhr.readyState === 4) {
            callback(xhr.responseXML);
        }
        };
        xhr.send(null);
    };

    //fetch the document
    fetchXML("http://localhost:8082/tmp/circle.svg",function(newSVGDoc){
        //import it into the current DOM
        var n = document.importNode(newSVGDoc.documentElement,true);
        document.documentElement.appendChild(n);

        var circle = document.getElementById("the_circle"); //now you have the circle
    }) 
    </script>
</svg>
Run Code Online (Sandbox Code Playgroud)


Spa*_*hut 11

您可以更轻松地访问必要的元素:

document.getElementById('the_image').contentDocument.getElementById('the_circle')
Run Code Online (Sandbox Code Playgroud)

查看 此图片以供参考(在dev.opera.com上获取) 在此输入图像描述