2016-09-22 66 views
0

我正在使用d3 v4并尝试将样式应用于mouseover上的元素。 我想知道,如果它与V4的API的变化,但我似乎无法获得在节点在mouseover上更改d3类

我有代码:

.on('mouseover',() => { 
    let self = d3.select(this); 
    let c = self.attr('class'); 

但是这给出了一个错误

Cannot read property 'getAttribute' of null 

所以d3.select()似乎没有工作... 然而,mouseover IS射击。

我可以使用

.on('mouseover', (elem) => { 
    console.log('elem', elem); 

哪位能给我一些类型D3的对象,而不是一个DOM节点。 我不能使用这个对象

elem.classed("hilite", true); 
    elem.attr("class", "hilite"); 

无论这些方法存在于D3返回的对象上的任何D3的方法。

那么我该如何在d3中做这个超级基本操作?

相关 Change class of one element when hover over another element d3

+0

如果这是打字稿,请您提供它所产生的(或者一个小提琴)的JavaScript代码? d3.select应该工作正常:https://jsfiddle.net/hjt5rxbx/ – Nixie

+0

你能为此做一个小提琴吗? –

+0

不需要小提琴,这只是在胖箭头功能中的“this”的含义。 –

回答

-1

您可以使用箭头功能(如果这个箭头的功能),如果你不需要当前元素的this访问。在这种情况下,您确实需要,因为D3调用事件处理程序将this设置为触发此事件的元素。

参见:Using arrow functions with d3