【问题标题】:Temporarily disable all currently active jQuery event handlers暂时禁用所有当前活动的 jQuery 事件处理程序
【发布时间】:2013-02-13 08:44:55
【问题描述】:

我正在使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;
}

但是当我双击可编辑 div 内的文本时,双击事件会传播到父 td,从而导致不良后果。我还想阻止其他事件(selectmousedown 等),因此为每个事件编写存根对我来说并不好。

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

【问题讨论】:

    标签: javascript jquery javascript-events


    【解决方案1】:

    或者以某种方式停止将所有事件从可编辑 div 传播到其父级?

    它可能不是很可口,但专门禁用事件也不是那么不好:

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

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

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

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

    【讨论】:

    • 如果td 的父母之一也有一些处理程序怎么办?我是否也为每个人制作了一个存根?
    • @warwaruk:不需要,false 停止传播。
    • 我有<td class="cell cell_selected"><div id="cell_editor" contenteditable="true">#</div></td>thistd: $(this).on("selectstart mousedown mouseup dblclick", false); 不仅为td 也为它的子div 关闭事件,所以我不能在 div 中选择文本,不能使用双击来选择单词等
    • @warwaruk:它没有,但如果你使用事件委托,它可能看起来是这样,因为通过委托你实际上将元素挂钩到一个祖先元素并依赖它冒泡到那个祖先.如果td 位于td 的子div 和您挂钩委托事件的祖先之间,则由于td 的处理程序停止传播,祖先永远不会看到该事件。您可以通过直接将事件挂钩到子级来处理它,或者通过查看td 上的事件处理程序中的e.target 并仅通过或不通过来选择性地禁用事件.
    【解决方案2】:

    使用开/关 JQuery 方法:

    var myFunction = 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;
    }
    
    //Active the events
    $(document).on("dblclick", "#table>tbody>tr>td.cell", myFunction);
    
    //Disable the events
    $(document).off("dblclick", "#table>tbody>tr>td.cell",myFunction);
    
    //Reactive the events
    $(document).on("dblclick", "#table>tbody>tr>td.cell", myFunction);
    

    更新

    如果必须不考虑事件,您还可以管理设置为 true 的变量:

    var skipEvent = true;
    
    $(document).on("dblclick", "#table>tbody>tr>td.cell", function (e) {
        //Check variable and skip if true
        if (skipEvent) 
            return false;
    
        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();
    });        
    

    【讨论】:

    • 我在问题中写道:“我还想阻止其他事件(select、mousedown 等),因此为每个事件编写一个存根对我来说并不好。”
    • 如果事件必须不考虑,您还可以管理设置为 true 的变量,请查看我的上次更新
    猜你喜欢
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多