首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >停止元素上的其他事件处理程序

停止元素上的其他事件处理程序
EN

Stack Overflow用户
提问于 2009-04-14 06:16:00
回答 2查看 27.3K关注 0票数 30

我正在写一个小的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中的单击函数来停止触发所有其他事件处理程序。我该怎么做呢?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/746506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档