暂时禁用所有当前活动的jQuery事件处理程序 [英] Temporarily disable all currently active jQuery event handlers

查看:97
本文介绍了暂时禁用所有当前活动的jQuery事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在双击时可以编辑表格的 TD 元素:

I am making a TD element of table editable on double click:

$(document).on("dblclick", "#table>tbody>tr>td.cell", function(e) {
    if (e.which != 1 || e.shiftKey || e.altKey || e.ctrlKey)
        // need left button without keyboard modifiers
        return;
    reset_selection();
    var editor = document.createElement("div");
    editor.setAttribute("contenteditable", "true");
    editor.innerHTML = this.innerHTML;
    this.innerHTML = '';
    // this.style.padding = 0;
    this.appendChild(editor);
    $(document).on("*", stub);
    editor.onblur = function() {
        // this.parentNode.setAttribute("style", "");
        this.parentNode.innerHTML = this.innerHTML;
        sys.editor = null;
        $(document).off("*", stub);;
    };
    editor.focus();
});

function stub(e) {
    e.stopImmediatePropagation();
    return false;
}

但是,当我双击可编辑div中的文本时,双击事件传播到父td导致不良后果。还有其他事件(选择 mousedown 等)我想阻止,所以为每个事件写一个存根他们看起来不太好。

But when i double click on the text inside the editable div, the double click event propagates to the parent td causing undesired consequences. There are also other events (select, mousedown, etc) i want to prevent, so writing a stub for each of them doesn't look nice to me.

有没有办法禁用所有当前活动的jQuery事件处理程序并在之后启用它们?或者有些人会停止将所有事件从可编辑div传播到其父级?

Is there a way to disable all currently active jQuery event handlers and enable them afterwards? Or somewhow stop propagating all events from the editable div to its parents?

推荐答案


或者某些事情会停止传播从可编辑div到其父母的所有事件?

Or somewhow stop propagating all events from the editable div to its parents?

它可能不太可口,但它不是 >不好专门禁用事件:

It may not be very palatable, but it's not that bad to specifically disable the events:

$(this).on("select mousedown mouseup dblclick etc", false);

(假设指的是你正在编辑的单元格。)

(Assuming this refers to the cell you're editing.)

毕竟,事件数量有限,且上的jquery.com/on> 允许您在空格分隔的字符串中列出它们,并通过传递 false

There is a limited number of events, after all, and on allows you to list them in a space-delimited string and disable them by passing false.

然后您可以通过传递相同的列表并再次将 false 重新启用它们来重新启用它们 off

You can then re-enable them by passing the same list and false again into off.

这篇关于暂时禁用所有当前活动的jQuery事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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