Accessing elements of embedded file

2019-08-22 16:04发布

I have a svg file which I am accessing through my javascript. Like this

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

I was in-lining svg before, so I was able to access all the elements of it. But then it (svg image) didnt render on safari. So, I employed this way. The image is now perfectly rendered, however I am not sure how to access elements of the svg file in the javascript. Can you give any suggestions.

Pasting my svg file code here:

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

I need to access the element "needle".

PS: I cant add anything in html. Everything has to be done on javascript side.

1条回答
来,给爷笑一个
2楼-- · 2019-08-22 16:43

I've created an example and pushed it to git repository. I've used the SVG image which you've provided in this question. It's working well, but there is one problem - the background image from img/gauge.png is not loading second time.

So current question seems answered, but the question with that image from SVG file is still open.

The work-around is to use the gauge.png image as a background-image of div element (which is a container for object element) and remove it from SVG at all.

Though I'll maybe play with it again later.

查看更多
登录 后发表回答