简体   繁体   English

如何获得g元素的宽度?

[英]How to get width of g element?

I am having some issue with getting width of <g></g> element using jQuery. 我在使用jQuery获取<g></g>元素的宽度时遇到一些问题。 Using .innerWidth() or width() returns me null. 使用.innerWidth()width()返回null。 Is it possible to get the <g></g> width using jquery? 是否可以使用jquery获得<g></g>宽度?

I am asking this, because of rendering differences between Chrome and IE. 我问这个问题是因为Chrome和IE之间存在渲染差异。 In chrome I simply use: 在Chrome中,我只需使用:

var box = svg.node().getBBox();
var width = box.width;

But in IE it doesnt work - it returns the widht of 0. 但是在IE中它不起作用-它返回的宽度为0。

Add Id to element Maybe work 将Id添加到元素也许可以工作

 <svg viewBox="0 0 95 50"
             xmlns="http://www.w3.org/2000/svg">
           <g stroke="green" fill="white" stroke-width="5" id="myGroup">
             <circle cx="25" cy="25" r="15"/>
             <circle cx="40" cy="25" r="15"/>
             <circle cx="55" cy="25" r="15"/>
             <circle cx="70" cy="25" r="15"/>
           </g>
        </svg>
        <script>
        $(document).ready(function(){
          var height =  document.getElementById("myGroup").getBBox().height;
          var width =  document.getElementById("myGroup").getBBox().width;
          alert(" width "+width+" height "+height);
        });
        </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM