简体   繁体   English

我如何使用d3js获取svg元素的高度

[英]how i get height of a svg element using d3js

i am using d3.js to find the height of an SVG element with the following code 我正在使用d3.js通过以下代码查找SVG元素的高度

<script>
    var body = d3.select("body");
    console.log(body);
    var svg = body.select("svg");
    console.log(svg);
    console.log(svg.style("height"));
</script>

<svg class="svg" height="3300" width="2550">
   <image x="0" y="0" height="3300" width="2550" xlink:href="1.jpg"></image>
   <rect class="word" id="15" x="118" y="259" width="182" height="28"
      text="Substitute"></rect>
</svg>

but it returned an error 但它返回了一个错误

Uncaught TypeError: Cannot call method 'getPropertyValue' of null 未捕获的TypeError:无法调用null的方法“ getPropertyValue”

You need to load js after the DOM is redered. 重新设置DOM之后,您需要加载js You can either put the <script></script> in the bottom of your <body></body> or have it inside the following javascript method. 您可以将<script></script>放在<body></body>的底部,也可以将其放在以下javascript方法中。

    document.addEventListener("DOMContentLoaded", function(event) { 
          //your code here
     });

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

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