简体   繁体   English

如何让一个事件调用多个功能? D3 / Javascript

[英]How to have one event call multiple functions ? D3/Javascript

I have these calls for when the mouse is over/off a node 当鼠标悬停在节点上时,我有这些要求

    .on("mouseover", highlightImage)
    .on("mouseout", unhighlightImage)

    .on("mouseover", showTextToolTip)
    .on("mouseout", hideTextToolTip)

Now, as javascript is asynchronously read only the bottom two functions get called. 现在,由于javascript是异步读取的,因此仅调用了底部的两个函数。 I have looked around and found a few examples: 我环顾四周,发现了一些示例:

.on("mouseover", "highlightImage(); showTextToolTip()") //- doesnt work

.on("mouseover", mouseOverFunctions) //- calls a function>

function mouseOverFunctions(){
    showTextToolTip();
    highlightImage();
    } //- also doesnt work.

I gather this is because the 'mouseover' can only call one object at a time. 我认为这是因为“ mouseover”一次只能调用一个对象。

How can I call both functions at once ? 如何同时调用这两个函数? I wish to show the text and highlight the node 我希望显示文本并突出显示节点


added code 添加代码

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);
}   

Define an anonymous function that calls them both: 定义一个同时调用它们的匿名函数:

.on("mouseover", function(d) {
  highlightImage(d);
  showTextToolTip(d);
});

and similarly for mouseout . 同样适用于mouseout

If you reference this in your handler functions, you need to use a different way of calling the functions to ensure that it is passed through properly: 如果在处理程序函数中引用this函数,则需要使用另一种调用函数的方法来确保其正确传递:

.on("mouseover", function(d) {
  highlightImage.call(this, d);
  showTextToolTip.call(this, d);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM