javascript - 使用 JavaScript 将子节点添加到内联 SVG。在 IE9 上不可能?

标签 javascript dom svg 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>

但是 SVGDocument.createTextNode("text"); 返回错误:对象不支持此属性或方法。我认为问题是我无法正确引用 idSVG 元素。我正在使用 IE9。

最佳答案

下面的例子适合我。

请注意,如果您不设置 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>

关于javascript - 使用 JavaScript 将子节点添加到内联 SVG。在 IE9 上不可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14749983/

相关文章:

javascript - Angular 翻译使用键而不是值来表示文本?

javascript - 异步谷歌分析 [Javascript 高尔夫]

Javascript 跨浏览器问题

javascript - 使用 javascript XMLHttpRequest 在 Firefox 扩展的本地主机上打开文件

javascript - 使用 jquery 验证插件验证登录并重定向到成功页面

javascript - jQuery 错误 : Invalid object initializer

javascript - 如何制作已从显示 :none to display:block available in the DOM? 更改的元素

html - SVG Angular 和 svg : prefix not working

css - 内联 SVG 在 IE 中不能用作背景图像

javascript - 在 D3 中放置多个图表