停止元素上的其他事件处理程序
我正在写一个小的jQuery扩展,阻止用户双击链接。
$.fn.preventDoubleClick = function() { return this.click(function() { var $t = $(this) , retVal = $t.data('active') // check the internal flag ; if (retVal || retVal === undefined) { // if ON... $t.data('active', false); // set the internal flag to OFF setTimeout(function() { $t.data('active', true); }, 1000); // after 1 second, set the internal flag to ON console.log("allowed"); return true; } else { // if OFF... console.log("blocked"); return false; } }); };
问题是如果元素上还有其他单击事件处理程序,它们仍会触发:
$('#myLink').click(function() { console.log("Clicked"); }); $('#myLink').preventDoubleClick();
现在当你双击链接时,我会在我的日志中看到这个:
允许
点击
受阻
点击
基本上,我需要在preventDoubleClick
使用clickfunction来阻止所有其他事件处理程序的触发。 我怎样才能做到这一点?
感谢Adam的链接,我能够看到我需要的function: stopImmediatePropagation()
。
我相信你正在寻找event.stopPropagation
编辑:事实certificate这不是尼克目的的正确选择。 请看他的回答 。