访问嵌入文件的元素

时间:2012-08-17 23:46:38

标签: javascript svg web

我有一个svg文件,我通过我的javascript访问。喜欢这个

 this.m_svg = new Element('embed');
this.m_svg.setAttribute("src","img/gauge.svg"); 

我之前在svg中,所以我能够访问它的所有元素。但后来它(svg图像)没有在safari上呈现。所以,我采用这种方式。图像现在完美呈现,但我不知道如何在javascript中访问svg文件的元素。你能提出任何建议吗?

在此处粘贴我的svg文件代码:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <g name="gauge" width="122px" height="127px">
        <image xlink:href="gauging.png" width="122" height="127"/>
    <circle id="led" cx="39" cy="76" r="5" style="fill: #999; stroke: none">
        <animateColor id="ledAnimation" attributeName="fill" attributeType="css" begin="0s" dur="1s"
        values="none;#f88;#f00;#f88;none;" repeatCount="0"/>
    </circle>
        <g id="needle" transform="rotate(0,62,62)">
            <circle cx="62" cy="62" r="4" style="fill: #c00; stroke: none"/>
            <rect transform="rotate(-130,62,62)" name="arrow"  x="58" y="38" width="8" height="24" style="fill: #c00; stroke: none"/>
            <polygon transform="rotate(-130,62,62)" points="58,39,66,39,62,30,58,39" style="fill: #c00; stroke: none"/>
        </g>
        <text id="value" x="51" y="98" focusable="false" editable="no" style="stroke:none; fill:#fff; font-family: monospace; font-size: 12px"></text>
    </g>
</svg>

我需要访问元素“needle”。

PS:我无法在html中添加任何内容。一切都必须在javascript端完成。

1 个答案:

答案 0 :(得分:2)

我创建了一个示例并将其推送到git repository。 我使用了您在this question中提供的SVG图像。 它运行良好,但有一个问题 - 来自img/gauge.png的背景图像未加载第二次。

所以当前的问题似乎得到了解答,但SVG文件中该图像的问题仍然存在。

解决方法是使用gauge.png图像作为div元素(它是object元素的容器)的背景图像,并将其从SVG中删除。

虽然我以后可能会再玩一次。

相关问题