最佳答案
我正在为一个 SVG 文件编写 ECMAScript/JavaScript,需要获取 text元素的 width和 height,这样我就可以调整围绕它的矩形的大小。在 HTML 中,我可以在元素上使用 offsetWidth和 offsetHeight属性,但是这些属性似乎不可用。
Here's a fragment that I need to work with. I need to change the width of the rectangle whenever I change the text but I don't know how to get the actual width (in pixels) of the text element.
<rect x="100" y="100" width="100" height="100" />
<text>Some Text</text>
有什么想法吗?