不能重新启用事件处理这样的。当您使用.off()
时,它将从元素中移除指定的事件处理程序。因此,如果您想重新注册处理程序,则需要再次将处理程序参考传递给.on()
方法。
,因为你需要使用handler基准多次,宁愿把它写一个独立的功能,以便它可以从多个地方被称为
你需要把它写这样
$('.menu-control').on('click', function() {
$('#module-container').fadeIn(300);
window.mtimer = setTimeout(function(){ $('#module-container').fadeOut(300); }, time);
//The following commented line doesn't work. I want to reallow the mousleave function, defined below (marked as HERE)
$('#module-container').on('mouseleave', mouseenter);
});
function mouseenter(){
clearTimeout(window.mtimer);
}
function mouseleave(){
window.mtimer = setTimeout(function(){ $('#module-container').fadeOut(300); }, time);
}
var time = 2000;
$('#module-container').on({
mouseenter: mouseenter,
//HERE
mouseleave: mouseleave
});
$('.stopme').on('click', function(e) {
$('#module-container').show().off('mouseleave');
e.preventDefault();
});