暂时禁用所有当前活动的jQuery事件处理程序

war*_*iuc 7 javascript jquery javascript-events

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

$(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;
}
Run Code Online (Sandbox Code Playgroud)

但是当我双击可编辑div内的文本时,双击事件会传播到父td,从而导致意外后果.还有其他我想要阻止的事件(select,mousedown等等),所以为每个事件编写一个存根对我来说并不好看.

在此输入图像描述

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

T.J*_*der 10

或者有些人会停止将所有事件从可编辑div传播到其父级?

它可能不太适合,但是专门禁用事件并不是那么糟糕:

$(this).on("select mousedown mouseup dblclick etc", false);
Run Code Online (Sandbox Code Playgroud)

(假设this是指您正在编辑的单元格.)

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

通过将相同的列表,然后,您可以重新启用它们false再次进入off.