2

次のような小さなサンプル ページを作成しました。

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>IGO&nbsp;&ndash;&nbsp;IRC Go</title>
        <link rel="stylesheet" type="text/css" href="igo.css" />
        <script type="text/javascript" src="igo.js"></script>
    </head>
    <body>
        <h1 onclick="makeCircle()">IGO&nbsp;&ndash;&nbsp;IRC Go</h1>
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="board" 
            width="4" height="4" viewBox="0 0 4 4">
        <!-- content will be generated by script -->
        </svg>
        <div id="foo"></div>
    </body>
</html>

igo.js次のようになります。

function makeCircle() {
    var circle = document.createElement("circle");
    var board = document.getElementById("board");
    circle.cx = 4;
    circle.cy = 4;
    circle.r  = 4;
    circle.fill = "lime";
    board.appendChild(circle);
}

問題は次のとおりです。機能しません。円は表示されません。手伝って頂けますか?

4

1 に答える 1

4

次のURLを確認してください:http: //blog.blazingcloud.net/2010/09/17/svg-scripting-with-javascript-part-1-simple-circle/

次のようなコードを使用する必要があります。

var c2 = document.createElementNS("http://www.w3.org/2000/svg", "circle");
c2.setAttribute("cx", "250");
c2.setAttribute("cy", "100");
c2.setAttribute("r", "60");
c2.setAttribute("fill", "#996699");
c2.setAttribute("stroke", "#AA99FF");
c2.setAttribute("stroke-width", "7");
mySvg.appendChild(c2);

現在、ブラウザで使用される属性ではなく、DOMオブジェクトを編集しています。

実例: http: //jsfiddle.net/CJrrz/

于 2013-01-18T18:16:54.817 に答える