2013-02-13 55 views
7

我正在上双击编辑表的TD元素:暂时禁用所有当前活动的jQuery的事件处理程序

$(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造成不良后果。还有其他的事件(select,mousedown等),我想要防止,所以为他们每个人写一个存根并不看好我。

enter image description here

有没有一种方法来禁用所有当前活动的jQuery的事件处理程序之后允许它们?或者有人停止将可编辑div的所有事件传播给其父母?

回答

10

或者某些人停止传播可编辑的所有事件div给它的父母?

它可能不是很可口,但它不是不好明确禁止的事件:

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

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

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

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

+0

如果'td'的父母之一也有一些处理程序会怎样?我是否也为他们每个人做了一个存根? – warvariuc 2013-02-13 08:57:15

+0

@warwaruk:不需要,'false'停止传播。 – 2013-02-13 08:58:47

+0

我有'

#
'。 'this'是'td':'$(this).on(“selectstart mousedown mouseup dblclick”,false);'不仅为'td'关闭事件,而且为其子'div'关闭事件,所以我不能选择文本在div中,不能使用双击来选择单词等 – warvariuc 2013-02-13 12:41:37

4

上使用开/关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(); 
});   
+1

我在问题中写道:“我还想要防止其他事件(select,mousedown等),所以为他们每个人写一个存根并不好看。” – warvariuc 2013-02-13 08:50:37

+0

如果事件一定不被考虑在内,你也可以管理一个设置为真的变量,查看我最近的更新 – sdespont 2013-02-13 08:54:31

相关问题