d3选择问题offsetParent为null

d3 selection issue offsetParent is null

本文关键字:null offsetParent 问题 选择 d3      更新时间:2023-09-26

我正试图使用d3选择获得offsetParent id(我以前有过类似的问题,但由于某种原因,答案在这里不起作用)。

当我控制台log d3.select(this)时,日志是:

[Array[1]]
    0: Array[1]
        0: g
          offsetParent: div#divID
              id: "divID"

如何从d3.select(this)开始获得id ?

我想做:

d3.select(d3.select(this).select("g").node().offsetParent).attr("id")

but get

Uncaught TypeError: Cannot read property 'offsetParent' of null

,当它不为空时。我一定选错了

g选择是导致问题的原因。这个选择:

d3.select(d3.select(this).node().offsetParent).attr("id")

为我解决了这个问题