【发布时间】:2012-01-13 09:19:55
【问题描述】:
我正在通过 javascript 将文本元素附加到 svg。附加后我想设置 x 和 y 坐标,但是,它在使用它来计算 x 时返回文本元素的错误宽度。
有趣: 在 Chrome 中,当通过 F5 或按钮实现页面时返回错误的宽度,当在地址栏中按 Enter 时,宽度是正确的 - 奇怪!
下面是小代码:
var capt = document.createElementNS("http://www.w3.org/2000/svg", "text");
// Set any attributes as desired
capt.setAttribute("id","capt");
capt.setAttribute("font-family","Righteous");
capt.setAttribute("font-size","30px");
capt.setAttribute("fill", "rgb(19,128,183)");
var myText = document.createTextNode(this.options.captTxt);
capt.appendChild(myText);
this.elements.jSvgElem.append(capt);
capt.setAttribute("x", this.options.windowWidth-this.options.spacer-document.getElementById("capt").offsetWidth);
capt.setAttribute("y", this.options.captY+$('#capt').height());
【问题讨论】:
标签: javascript text svg width element