2014-12-05 109 views
1

我有這些呼籲,當鼠標在開/關的節點如何讓一個事件調用多個函數? D3/JavaScript的

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

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

現在,JavaScript是異步只讀底部的兩個函數被調用。我環顧四周,發現了幾個例子:

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

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

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

我推測,這是因爲「鼠標懸停」只能在同一時間撥打一個對象。

如何同時調用兩個函數?我希望顯示文字和突出節點


添加代碼

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

回答

4

定義一個匿名函數來調用他們兩個:

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

,同樣爲mouseout

如果您在處理函數引用this,您需要使用電話的功能,以確保它通過正確地傳遞不同的方式:

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

類型錯誤:e.getAttribute不是一個函數? :/ @lars kotthoff – rekoDolph 2014-12-05 16:00:44

+0

錯誤來自哪裏? – 2014-12-05 16:05:24

+0

javascript - d3.v3.min.js(line 1,col 3091) – rekoDolph 2014-12-05 16:09:46

相關問題