我目前正在使用 JavaScript 中的 svg
元素。我对此很陌生。
我的问题是我有一个 svg
元素,其中有多个 svg:g
元素。在我的 svg:g
元素中,我有各种其他 svg 元素。
<svg>
<g class="my-class">
<g class "c1">
<text style="font-size: 9" x="0" y="-4"> john </text>
<text style="font-size: 9" x="70" y="-4"> 13 </text>
</g>
<g class="c2">
<text style="font-size: 9" x="0" y="-4"> john </text>
<text style="font-size: 9" x="70" y="-4"> 13 </text>
</g>
<g class="c3">
<text style="font-size: 9" x="0" y="-4"> john </text>
<text style="font-size: 9" x="70" y="-4"> 13 </text>
</g>
</g>
</svg>
g
正在动态追加到我的
g "my_class"
现在我想将我的 svg
宽度设置为等于 g.my_class
宽度的宽度。
var svgWidth = $('.my-class').width()
alert(svgWidth);
但它给了我零。我怎么能在我的浏览器上的黄色工具提示框中看到它
当我选择这一行时。
谁能指导一下?我这样做对吗,或者我怎样才能做到这一点? 谢谢
最佳答案
试试 .getBoundingClientRect
$('.my-class')[0].getBoundingClientRect().width;
关于javascript - 如何获取 svg :g element 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702952/