1

このHTMLコードがあり、JavaScriptコードを呼び出しています。コードはゲージ用です。javascriptコードで、SVGファイルにアクセスし、(ゲージの)針を変更して目的の値を表示しようとしています。コードは正常に機能しています。ただし、onload()を呼び出したくありません。HTMLの「オブジェクトID」。HTMLファイルが読み込まれるとすぐに、javascriptが自動的に呼び出されるようにしたいだけです。bodyタグの見出しを除いて、htmlで何も呼び出したくない(オブジェクトID、onload関数なし)。さまざまなonload()メソッドを試しましたが、このリンクも試しました http://www.techrepublic.com/article/preloading-and-the-javascript-image-object/5214317 しかし、実装できません。どんな助けでも大歓迎です。

PS:私は問題を徹底的に説明するために最善を尽くしました。ただし、どこか不明な点がありましたらお知らせください。

これは、下に貼り付けたsvgコードに埋め込まれているGauge.png画像です https://sphotos-b.xx.fbcdn.net/hphotos-snc6/179594_10150982737360698_1827200234_n.jpg

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

HTML+Javascriptコード

<head>
<title>SVG Gauge example</title>

<script>
function update1(){
    var scale=100;
    var value;
    var value1 = 69;

    var el=document.getElementById('gauge1');       
    if (!el) return;

    /* Get SVG document from HTML element */
    var svg_doc = el.contentDocument;
    if (!svg_doc) return;


    /* Rotate needle to display given value */
    var needle_el = svg_doc.getElementById('needle');
    if (!needle_el) return;
    /* Calc rotation angle (0->0%, 260->100%) */
    value = parseInt(value1);
    scale = parseInt(scale);
    if (value > scale) value = scale;
    var angle = value / scale * 260;
    /* On-the-fly SVG transform */
    needle_el.setAttribute('transform','rotate('+angle+',62,62)');
}
</script>

</head>
<body onload="update1(); ">
<div>
<object id="gauge1" type="image/svg+xml" data="gauge.svg" width="127" height="122"/>
</div>
</body>    

</html>
4

1 に答える 1

1

属性を回避するには、次のイベントonloadを使用できます。load

<script type="text/javascript">
    function update1() {
        // ...
    }

    document.addEventListener('load', update1, true);
</script>
于 2012-07-30T20:53:56.407 に答える