0

I have been trying to render a svg image through javascript. Tried various options, but ultimately have to resolve to this one.

I have declared an object id in html like this

<object id="gauge1" type="image/svg+xml" data="gauge.svg" width="127" height="122"/>

Now, I have been trying to access this gauge.svg through javascript. I have created an element of embed type like this

this.m_svg = document.createElement('embed');

aand then trying to use setAttribute to access the image, but no success. Any suggestions?

Also pasting the svg code here with the image.

image: https://sphotos-b.xx.fbcdn.net/hphotos-snc6/179594_10150982737360698_1827200234_n.jpg svg code:

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

PS : Already tried this.m_svg.innerHTML = svg code (Doesnt work on safari)

4

2 回答 2

2

This shows you how to access the elements that are the contents of an <object> tag.

于 2012-08-20T15:24:04.317 回答
0

Make sure that your headers are set correctly, I know that webkit can get a little selective with it's SVG implementation unless headers are set to:

<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />

for more info check here: https://stackoverflow.com/a/6410814/873836

于 2012-08-20T15:21:53.537 回答