2013-02-13 14 views
7

私はダブルクリックで編集可能なテーブルのTD要素を作っていますを無効にします親は望ましくない結果を引き起こす。また、他のイベント(selectmousedownなど)があります。そのため、それぞれのスタブを書くことは私にはうれしくありません。は、一時的に現在アクティブなすべてのjQueryのイベントハンドラ

enter image description here

現在アクティブなすべてのjQueryのイベントハンドラを無効にし、その後、それらを有効にする方法はありますか?または、編集可能なdivからその親にすべてのイベントを何らかの形で伝播させないでください。

答えて

10

または、編集可能なすべてのイベントを何らかの形で伝播しないようにするその両親に分割しますか?

それは非常においしいではないかもしれないが、それは、具体的なイベント無効に悪いことではありません。

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

は(thisはあなたが編集している細胞を意味すると仮定。)

イベント数は限られています。onは、空白で区切られた文字列にリストし、falseを渡して無効にすることができます。

その後、同じリストを渡してfalseを再度有効にして、offに再度有効にすることができます。

+0

'td'の両親にもハンドラがあるとどうなりますか?私もそれぞれのスタブを作っていますか? – warvariuc

+0

@warwaruk:必要はありません。「false」は伝播を停止します。 –

+0

私は '

#
'を持っています。 'これは 'td':' $(this)).on( "selectstartマウスアップdblclick"、false); 'td'だけでなくその子' div'のイベントもオフにするので、選択できませんdivのテキストをダブルクリックして単語などを選択することはできません – warvariuc

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

私は「他のイベント(選択、マウスダウンなど)も防止したいので、それぞれのスタブを書くことは気に入らない」と書いています。 – warvariuc

+0

イベントを考慮してはならない変数をtrueに設定して管理することもできます。最後の更新を確認してください – sdespont

関連する問題