【问题标题】:D3 cannot read property offsetHeight of nullD3 无法读取 null 的属性 offsetHeight
【发布时间】:2018-11-11 10:20:36
【问题描述】:

当我刚刚初始化它的高度时,我得到了这个cannot read property 'offsetHeight' of null

这是我的代码:

d3.select("#container")
    .style("height", "100%")
    .style("width", "100%") 
    .style("position", "relative")
    .style("bottom", 0);


var width = document.getElementById("#container").offsetWidth,
    height = document.getElementById("#container").offsetHeight,
    radius = (Math.min(width, height) / 1.5);

【问题讨论】:

    标签: javascript d3.js null offsetwidth offsetheight


    【解决方案1】:

    使用 document.getElementById() 时,您不必在 id 前显式键入 #

    【讨论】:

    • 我知道,这并不能解决我的问题 :(
    • 如果在使用document.getElementById("container") 时抛出同样的错误,我会检查它是否返回任何东西。如果不存在,则该元素要么根本不存在,要么在执行代码时文档还没有准备好。根据您描述的错误,我猜这不是 d3.js 的问题,而是最后三行的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2016-11-24
    相关资源
    最近更新 更多