SVG文本元素未在Android浏览器中显示

时间:2016-01-02 22:13:16

标签: javascript svg

我正在尝试使用下面的javascript代码将svg图形嵌入到我的页面中,但是当我在我的手机上使用Android的默认浏览器名为“Internet”版本5.8查看网页时,圆圈正在显示,但是文本没有出现。它在其他浏览器上运行良好,但我担心它可能不会出现在某些Safari浏览器上。我在这做错了什么?请注意,当我将输出svg代码复制并粘贴到我的源文件中并打开它时,文本会显示出来,所以我很确定javascript存在一些问题。

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);

1 个答案:

答案 0 :(得分:1)

创建文本节点的常用方法是通过

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

而不是innerHTML

这应该适用于Android的默认浏览器,并且也可以在其他任何地方使用。

相关问题