2013-02-13 50 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

相關問題