使用JavaScript将子节点添加到内联SVG.在IE9上不合适?

Jes*_*rez 2 javascript svg dom internet-explorer-9

我只想使用accion()javaScript函数在html上的SVG中添加一个新的文本节点:

<!DOCTYPE> 
<html>
<body>
<head>
<script type="text/javascript"> 
    function accion(){
        var SVGDocument = document.getElementById("idSVG");
        var text2 = SVGDocument.createTextNode("text");
        SVGDocument.appendChild(text2);
}       
</script>
</head>

<input type="button" onclick="accion()" value="GO!!"/>

<svg id="idSVG">
   <text id="texto" x="50" y="35" font-size="14">PRUEBA</text>
</svg>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

但是SVGDocument.createTextNode("text");返回一个错误:Object does not support this property or method.我认为问题是我无法正确引用idSVG元素.我正在使用IE9.

Rob*_*son 11

以下示例适用于我.

请注意,如果未设置y坐标,则默认值为0,0可能位于svg边界框之外.

<!DOCTYPE> 
<html>
<body>
<head>
<script type="text/javascript"> 
    function accion(){
        var svg = document.getElementById("idSVG");
        var text2 = document.createElementNS("http://www.w3.org/2000/svg", "text");
        text2.setAttribute("y", "100");
        var textContent = document.createTextNode("this is the text content");
        text2.appendChild(textContent);
        svg.appendChild(text2);
}       
</script>
</head>

<input type="button" onclick="accion()" value="GO!!"/>

<svg id="idSVG">
   <text id="texto" x="50" y="35" font-size="14">PRUEBA</text>
</svg>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)