2016-01-02 42 views
3

我想使用下面的JavaScript代碼將svg圖形嵌入到我的頁面中,但是當我在Android手機上使用Android默認瀏覽器命名爲「Internet」版本5.8時,圓圈正在顯示,但文字沒有顯示出來。它在其他瀏覽器上運行良好,但我擔心它可能不會在某些Safari瀏覽器上顯示。我在這裏做錯了什麼?請注意,當我複製並粘貼輸出svg代碼到我的源文件並打開它時,文本將顯示出來,所以我非常肯定JavaScript有一些問題。在Android瀏覽器中未顯示SVG文本元素

var svgtag=document.createElementNS('http://www.w3.org/2000/svg','svg'); 
svgtag.setAttribute('height','500'); 
svgtag.setAttribute('width','500'); 
document.getElementById("piechart").appendChild(svgtag); 

var circle=document.createElementNS('http://www.w3.org/2000/svg','circle'); 
circle.setAttribute('cx','250'); 
circle.setAttribute('cy','250'); 
circle.setAttribute('r','200'); 
circle.setAttribute('fill','#999'); 
svgtag.appendChild(circle); 

var sample=document.createElementNS('http://www.w3.org/2000/svg','text'); 
sample.setAttribute('x','250'); 
sample.setAttribute('y','250'); 
sample.setAttribute('font-size','12'); 
sample.setAttribute('fill','#000'); 
sample.innerHTML='someting'; 
svgtag.appendChild(sample); 
+0

使用createTextNode代替innerHTML工作!非常感謝 – dshukertjr

回答

1

通常的方法來創建一個文本節點是通過

text = document.createTextNode("someting"); 
sample.appendChild(text); 

代替的innerHTML

這應該在Android上的默認瀏覽器工作,還將努力在其他地方了。