我知道在JavaScript中,“this”的含义与TypeScript中的不同,如此文章所述TypeScript中的“this”。我有以下JavaScript代码用于在选定节点上创建更粗的描边,并使所有其他节点的描边更小。
node.on('click', function (d) {
d3.selectAll('circle').attr('stroke-width', 1.5);
d3.select(this).select('circle').attr('stroke-width', 5);
})
在 TypeScript 中,我有
this.node.on('click', (d:any) => {
this.node.selectAll('circle').attr('stroke-width', 1.5);
[this is where I need help].select('circle').attr('stroke-width', 5);
}
d
对象,也许它与第一个片段中的this
一样引用了节点。 - dfsqd
对象(第一个参数)无法获取 DOM 元素。但是,第二个和第三个参数可以轻松地用于获取this
(在 D3 方法中表示当前的 DOM 元素)。 - Gerardo Furtado