当鼠标悬停在节点上时,我有这些要求
.on("mouseover", highlightImage)
.on("mouseout", unhighlightImage)
.on("mouseover", showTextToolTip)
.on("mouseout", hideTextToolTip)
Run Code Online (Sandbox Code Playgroud)
现在,由于javascript是异步读取的,因此仅调用了底部的两个函数。我环顾四周,发现了一些示例:
.on("mouseover", "highlightImage(); showTextToolTip()") //- doesnt work
.on("mouseover", mouseOverFunctions) //- calls a function>
function mouseOverFunctions(){
showTextToolTip();
highlightImage();
} //- also doesnt work.
Run Code Online (Sandbox Code Playgroud)
我认为这是因为“ mouseover”一次只能调用一个对象。
如何同时调用这两个函数?我希望显示文本并突出显示节点
添加代码
function showTextToolTip(d){
if(textButtonPressed==false){
d3.select(this).append("text")
.attr("dx", "2")
.attr("dy", "-24")
.style("text-anchor", "start")
.text(function(d) { return d.identifier; });
showToolTip="true";
}}
function highlightImage(d){
d3.select(this).classed("highlighted", true);
highlightedNode = d.coreId;
//console.log("Highlighted node : " + highlightedNode);
}
Run Code Online (Sandbox Code Playgroud)
定义一个同时调用它们的匿名函数:
.on("mouseover", function(d) {
highlightImage(d);
showTextToolTip(d);
});
Run Code Online (Sandbox Code Playgroud)
和类似的mouseout
。
如果this
在处理程序函数中引用,则需要使用其他方法来调用函数以确保其正确传递:
.on("mouseover", function(d) {
highlightImage.call(this, d);
showTextToolTip.call(this, d);
});
Run Code Online (Sandbox Code Playgroud)