2012-10-31 37 views
2

是否有可能將hover()綁定到帶有指定選擇器的文檔,以便爲具有匹配屬性的動態添加內容分配JS函數?jQuery綁定懸停到「文檔」

我一直在尋找這兩個主題:JQuery .on() method with multiple event handlers to one selectorIs it possible to use jQuery .on and hover?

,目前我使用:

$('.profile-gallery-image-container').on({ 
    mouseenter: function(){ 
     $('.delete-image', this).show(); 
     $('.image-options', this).show(); 
    }, 
    mouseleave: function(){ 
     $('.delete-image', this).hide(); 
     $('.image-options', this).hide(); 
    } 
}, '.profile-gallery-image-container'); 

但沒有能夠與選擇匹配傳遞功能,新內容。

回答

7

在委託事件方法中,您應該將事件綁定到.profile-gallery-image-container的一個父元素。一種可能的解決方案是將其綁定到<body>(或document)元素:

$('body').on({ 
    mouseenter: function(){ 
     $('.delete-image', this).show(); 
     $('.image-options', this).show(); 
    }, 
    mouseleave: function(){ 
     $('.delete-image', this).hide(); 
     $('.image-options', this).hide(); 
    } 
}, '.profile-gallery-image-container'); 
+0

好的,謝謝。學到了新東西。與文件一起工作。 – user1683645

+1

@ user1683645不客氣! – VisioN