7

テーブルの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に伝播し、望ましくない結果を引き起こします。他にも防止したいイベント(select、、mousedownなど)があるので、それぞれにスタブを書くのは見栄えがよくありません。

ここに画像の説明を入力してください

現在アクティブなすべてのjQueryイベントハンドラーを無効にして、後で有効にする方法はありますか?または、どういうわけか、編集可能なdivからその親へのすべてのイベントの伝播を停止しますか?

4

2 に答える 2

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();
});        
于 2013-02-13T08:49:50.303 に答える