2012-06-20 40 views
3

我有以下的jQuery代碼,試圖實現對event.`jQuery的mouseon /鼠標移開或內徘徊「開」綁定事件

$(".editable_template_region").on({"mouseover":function(){ 
     $('<a href="javascript:void(0)" class="hoverItTemplate">click to edit</a>').appendTo($(this)); 

    }, "mouseout" : function() { 
     $(this).find(".hoverItTemplate").remove(); 
    }});` 

我的綁定與時不工作很正確不要認爲這段代碼是正確的,因爲這會導致事件「閃爍」或反覆循環。所以我的懸停課只是閃爍着。

這是我以前使用過的代碼,但我想將它切換到on事件以獲得更好的綁定。

 $(".editable_template_region").hover(function() { 
     $('<a href="javascript:void(0)" class="hoverItTemplate">click to edit</a>').appendTo($(this)); 
    }, function() { 
     $(this).find(".hoverItTemplate").remove(); 
    });  

在此先感謝。

回答

3

試試這個:

$(".editable_template_region").on({ 
    mouseenter: function() { 
     $('<a href="javascript:void(0)" class="hoverItTemplate">click to edit</a>').appendTo($(this)); 
    }, 
    mouseleave: function() { 
     $(this).find(".hoverItTemplate").remove(); 
    } 
});​ 

我不知道你的意思是「更好的結合」,但上面的,或者你的計劃.hover(應該都工作)。

+0

這按預期工作。謝謝你的例子。 –