我正在编写一个小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
中的click函数来阻止所有其他事件处理程序的触发。我怎么能这样做?
答案 0 :(得分:35)
感谢Adam的链接,我能够看到我需要的功能:stopImmediatePropagation()
。
答案 1 :(得分:6)
我相信你正在寻找event.stopPropagation
编辑:事实证明这不是尼克目的的正确选择。请参阅his answer。