在元素上停止进一步的事件处理程序 [英] Stop further event handlers on an element

查看:84
本文介绍了在元素上停止进一步的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个jQuery扩展名,阻止用户双击链接。

  $。fn。 preventDoubleClick = function(){
return this.click(function(){
var $ t = $(this)
,retVal = $ t.data('active')// check内部标志
;
if(retVal || retVal === undefined){//如果ON ...
$ t.data('active',false); //设置内部标志为OFF
setTimeout(function(){
$ t.data('active',true);
},1000); // 1秒后,设置内部标志到$ ON
console.log(allowed);
return true;
} else {//如果OFF ...
console.log(blocked);
return false;
}
});
};

问题是如果元素上有其他点击事件处理程序,它们仍然会触发:点击(function(){
console.log(Clicked); $($)$ $ $ $ $ $ $
});
$('#myLink')。preventDoubleClick();

现在,当您双击链接时,我将其记录在我的日志中:


允许

clicked

blocked

clicked


所以基本上,我需要 preventDoubleClick 中的点击功能来停止所有其他事件处理程序的触发。如何做到这一点?

解决方案

感谢Adam的链接,我看到了我需要的功能: stopImmediatePropagation()


I'm writing a little jQuery extension that prevents a user from double clicking on a link.

$.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;
        }
    });
};

the problem is that if there are other click event handlers on the elements, they still fire:

$('#myLink').click(function() {
    console.log("Clicked");
});
$('#myLink').preventDoubleClick();

And now when you double click the link, I get this in my log:

allowed
clicked
blocked
clicked

So basically, I need the click function inside preventDoubleClick to stop all the other event handlers from firing. How can I do this?

解决方案

Thanks to Adam's link, I was able to see the function I needed: stopImmediatePropagation().

这篇关于在元素上停止进一步的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆