如何在此处停止无限递归,以便只触发一次 mousedown 事件?它应该在一个单独的方法中吗?
$(document).on('mouseover',function (e) {
$(e.target).mousedown(function(e){
e.stopPropagation();
console.log('clicked');
$(e.target).trigger('mousedown'); //use hello(e); instead?
return;
})
});
$(document).on('mousedown', 'a', function(e){
hello(e);
});
function hello(e){
console.log('got it');
}
这似乎触发了一个永无止境的循环。基本上我需要在鼠标下的当前元素上绑定(bind)一个 mousedown 处理程序,这将触发另一个处理程序能够处理的 mousedown 事件。
我这样做的原因是因为鼠标悬停在动态生成的元素上工作,所以当发生这种情况时,我需要再次绑定(bind)处理程序,因为 on
处理程序无法捕获新生成的元素。
最佳答案
你把事情搞复杂了。想象一下,每次用户移动他的鼠标时,都会动态添加一个事件?
为什么不使用类名设计您的元素并在您的 mousedown 事件中使用它?这样,对 mousedown 的肯定调用将始终触发。
$(".className").mousedown(function(e){
console.log('clicked');
return;
})
--我这样做的原因是因为鼠标悬停在动态生成的元素上工作,所以当发生这种情况时,我需要再次绑定(bind)处理程序,因为 on 处理程序无法捕获新生成的元素。
如果您这么说,那么请将一个类名添加到动态生成的元素中,以便将您的 mousedown 事件绑定(bind)到它。
关于javascript - jquery:停止无限递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27664184/