如何获得 SVG 组元素的高度/宽度? [英] How does one get the height/width of an SVG group element?
本文介绍了如何获得 SVG 组元素的高度/宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道 SVG 元素的宽度和高度吗?我正在尝试使用以下内容:
I need to know the width and height of a SVG element? Im trying to use the following:
$('g#myGroup').height()
...但结果总是零?
推荐答案
svg <g>
元素没有明确的高度和宽度属性,它们会根据所包含的内容自动调整大小.您可以通过在元素上调用 getBBox 来获取它们的实际高度/宽度:
svg <g>
elements don't have explicit height and width attributes, they auto size to whatever they contain. You can get their actual height/width by calling getBBox on the element though:
var height = document.getElementById("myGroup").getBBox().height;
如果你真的很喜欢 jquery,你可以把它写成
If you're really into jquery you could write it as
$('g#myGroup').get(0).getBBox().height;
根据簧片线轴
这篇关于如何获得 SVG 组元素的高度/宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文