嗨我动态添加一些元素,现在我试图绑定和hover()使用,on()但似乎不适用于回调函数.有任何想法吗?
jQuery:
$(document.body).on('hover', 'div.settings-container', function () {
$(this).find('ul.settings-links').fadeIn();
}, function () {
$(this).find('ul.settings-links').fadeOut();
});
Run Code Online (Sandbox Code Playgroud)
所述的jsfiddle被简化.
在jQuery中,$(selector).hover(handlerIn, handlerOut) 只是一个快捷方式
$(selector).mouseenter(handlerIn).mouseleave(handlerOut);
Run Code Online (Sandbox Code Playgroud)
hover不是一个事件,你需要使用mouseenter而mouseleave不是.
$('body').on({
mouseenter: function() {
$(this).find('ul.settings-links').fadeIn();
},
mouseleave: function() {
$(this).find('ul.settings-links').fadeOut();
}
}, 'div.settings-container');
Run Code Online (Sandbox Code Playgroud)