2013-02-07 28 views
2

我只是想在SVG中添加一個新的文本節點上使用HTML安信永()JavaScript函數:使用JavaScript將子節點添加到內聯SVG。不可能在IE9上?

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

+0

_What_error ..? –

+0

對象不支持此屬性或方法。 – deimos1975

+0

顯然你使用的是IE9。我不認爲IE9支持這樣的SVG,是嗎? –

回答

11

下面的例子適用於我。

請注意,如果您未設置y座標默認值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> 
+0

沒關係。謝謝!。我不能投票給你。沒有足夠的聲譽:(。希望有人做! – deimos1975

相關問題