[英]Add a child node to an inline SVG with JavaScript. Imposible on IE9?
我只想使用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");
返回錯誤: Object does not support this property or method
。 我認為問題是我無法正確引用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.