2010-05-07 43 views
0

我想使用mousehover jQuery事件調用javascript函數。使用jQuery在Mousehoever和mouseout上調用javascript函數

這是一個功能。

// On mouse hover function  
function ShowHighlighter(d, highlight_elid) { 
      //alert ("ShowHighlighter\n"+d); 
      if (d == "addHighlight") { 
       var txt = getSelText(); 
       if (txt.length < 1) 
       { 
        return; 
       } 
       ShowContent(d); 
      } else if (d == "deleteHighlight") { 
       var elid = "#"+d; 
       jQuery(elid).stop(); 
       ShowContent(d); 
       delete_highlight_id = "#"+highlight_elid; 
      } 
     } 


// on Mouse out function 
function HideContent(d) { 
     if(d.length < 1) { return; }  
     document.getElementById(d).style.display = "none"; 
    } 

我想使用此功能...但它似乎並沒有工作。

jQuery('a[href="HIGHLIGHT_CREATELINK"]').mouseover(ShowHighlighter("deleteHighlight","highlight"+ randomCount + ");) ; 
     jQuery('a[href="HIGHLIGHT_CREATELINK"]').mouseout('javascript:HideContentFade(\"deleteHighlight\");') 

請幫我在這裏。

謝謝。

回答

1

有對懸停事件的速記方法:http://api.jquery.com/hover/

jQuery('a[href="HIGHLIGHT_CREATELINK"]').hover(function() { 
    // this is the mouseover handler 
    ShowHighlighter("deleteHighlight","highlight"+ randomCount + "); 
}, 
function() { 
    // this is the mouseout handler 
    HideContentFade("deleteHighlight"); 
}); 
2

您可以使用hover函數縮短語法:

jQuery('a[href="HIGHLIGHT_CREATELINK"]').hover(function(evt) { 
    ShowHighlighter("deleteHighlight", "highlight" + randomCount); 
}, function(evt) { 
    HideContentFade("deleteHighlight"); 
}); 
0

試着這樣做:在jQuery的事件綁定到一個元素時

$('a[href="HIGHLIGHT_CREATELINK"]').mouseover(function(){ 
      ShowHighlighter("deleteHighlight","highlight"+ randomCount + "); 
    }) ; 
    $('a[href="HIGHLIGHT_CREATELINK"]').mouseout(function(){ 
      HideContentFade("deleteHighlight"); 
    }); 

出於某種原因,你必須使用上面的語法,而不是嘗試和直接綁定你的功能。

+2

上的每一mousemovement「的mouseenter」和「鼠標離開」鼠標懸停火災是也許是更好的事件 – jAndy 2010-05-07 06:36:34

+0

你提到的_reason_是因爲該功能,他希望被調用需要特定的參數,所以他需要將其包裝在另一個函數中。 jQuery無法猜測'ShowHighlighter'期望的參數。此外,答案中提供的代碼調用'ShowHighlighter'並將其返回值傳遞給'mouseover'函數,這顯然是錯誤的。 – jweyrich 2010-05-07 06:44:55

+0

爲什麼它錯了? – CeejeeB 2010-05-10 13:32:25

0

您需要附上功能於一身的匿名函數

像這樣

jQuery('a[href="HIGHLIGHT_CREATELINK"]').mouseover(function() { 
    ShowHighlighter("deleteHighlight","highlight"+ randomCount + "); 
}); 

jQuery('a[href="HIGHLIGHT_CREATELINK"]').mouseout(function() { 
    HideContentFade("deleteHighlight"); 
}); 

,如果你不這樣做,該函數將被立即執行,而不是相加的處理程序,事件。

+0

鼠標移動觸發每一個鼠標移動,'mouseenter'和'mouseleave'可能是更好的事件 – jAndy 2010-05-07 06:36:51