我通过javascript创建SVG元素,它工作正常,但是当我创建一个文本svg元素并定义它的内容时,浏览器不会呈现值,尽管值是在代码中,当我用萤火虫检查它时。通过javascript动态设置文本svg元素
的代码是:
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('xlink','http://www.w3.org/1999/xlink');
svg.setAttribute('width','187');
svg.setAttribute('height','234');
var rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
rect.setAttribute('width','187');
rect.setAttribute('height','234');
rect.setAttribute('fill','#fff');
rect.setAttribute('stroke','#000');
rect.setAttribute('stroke-width','2');
rect.setAttribute('rx','7');
var text = document.createElementNS('ttp://www.w3.org/2000/svg', 'text');
text.setAttribute('x', '10');
text.setAttribute('y', '20');
text.setAttribute('fill', '#000');
text.textContent = '2';
svg.appendChild(rect);
svg.appendChild(text);
var wp = document.getElementById('wrapper');
wp.appendChild(svg);
这里是的jsfiddle链接http://jsfiddle.net/sAhyC/ 如果你检查,你会看到文本元素存在的价值SVG,但浏览器不显示它。
感谢
我的猜测是“动态”不是这里的问题。 –