我正在使用d3js来查找svg元素的宽度,给出的代码如下:
<script>
var body = d3.select("body");
console.log(body);
var svg = body.select("svg");
console.log(svg);
console.log(svg.style("width"));
</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>
但它返回了这个错误:
未捕获的类型错误:无法调用null的'getPropertyValue'方法
我认为,svg变量是一个空数组。
如何使用d3js获取svg元素的宽度?