SVG文本元素在安卓浏览器中无法显示。

3
我正在尝试使用以下JavaScript代码将SVG图形嵌入到页面中,但当我在我的手机上使用名为“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);

使用createTextNode而不是innerHTML有效了!非常感谢。 - dshukertjr
1个回答

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

标签。

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

使用innerHTML的替代方案

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接