2014-02-23 239 views
1

我有一個簡單的javascript mouseover事件來觸發asp菜單上的c#click事件,它工作正常,但是當我輸入menuitem時,click事件被觸發無限次。我希望它只能射擊一次。點擊鼠標懸停

<script type="text/javascript"> 
$(".div1").mouseover(function() { 
    $(this).click(); 
}) 
.mouseleave(function() { 
    $("#Menu1").remove(); 
    $("#Menu2").remove(); 
    $("#Menu3").remove(); 
}); 
</script> 

回答

2

你可以做到這一點有兩種方式:

第一種方式:

$(".div1").mouseover(function() { 
    $(this) 
     .click() 
     .off('mouseover'); // Unbind the mouseover once we triggered the click 
}) 

第二種方法 - 使用 「一是」 結合觸發事件處理只有一次

$(".div1").one('mouseover', function() { 
    $(this).click(); 
}) 
+0

他們沒有工作,兩個。 – Amr

+0

它必須工作,你應該給我們更詳細的代碼,或者更好的顯示問題本身的jsFiddle。 –

+0

我無法在jsFiddle中編寫c#單擊事件,我甚至不知道它是否可能,但我非常肯定jQuery調用它是問題。 – Amr

0

你可以試試這個:

var done = false; 
$('.div1').mouseover(function() 
{ 
    if(!done) 
    { 
     $(this).click(); 
     done = true; 
    } 
}); 
0

使用unbind jQuery的功能:

var fireClick = function() { 
    $(this).click(); 
    $(this).unbind('mouseover'); 
}; 

$(".div1").mouseover(fireClick) 
.mouseleave(function() { 
    $("#Menu1").remove(); 
    $("#Menu2").remove(); 
    $("#Menu3").remove(); 
    $(this).mouseover(fireClick); 
});