3

職場にダッシュボードがあり、タブのテキストを変更して、自分に最適なカテゴリを作成できるようにしたいと考えています。

または の変更を無視するEscか、変更をコミットする1 行のテキストがEnter必要Blurでした。

答えは見つかりましたが、他の人を助けるために投稿することにしました。

4

1 に答える 1

0

HTML マークアップは次のとおりです。

<span contenteditable="false"></span>

jQuery/javascript は次のとおりです。

$(document).ready(function() {
    $('[contenteditable]').dblclick(function() {
        $(this).attr('contenteditable', 'true');
        clearSelection();
        $(this).trigger('focus');
    });

    $('[contenteditable]').live('focus', function() {
        before = $(this).text();
        if($(this).attr('contenteditable') == "true") { $(this).css('border', '1px solid #ffd646'); }
    //}).live('blur paste', function() {
    }).live('blur', function() {
        $(this).attr('contenteditable', 'false');
        $(this).css('border', '1px solid #fafafa');
        $(this).text($(this).text().replace(/(\r\n|\n|\r)/gm,""));

        if (before != $(this).text()) { $(this).trigger('change'); }
    }).live('keyup', function(event) {
        // ESC=27, Enter=13
        if (event.which == 27) {
            $(this).text(before);
            $(this).trigger('blur');
        } else if (event.which == 13) {
            $(this).trigger('blur');
        }
    });

    $('[contenteditable]').live('change', function() {
        var $thisText = $(this).text();
        //Do something with the new value.
    });
});

function clearSelection() {
    if ( document.selection ) {
        document.selection.empty();
    } else if ( window.getSelection ) {
        window.getSelection().removeAllRanges();
    }
}

これが誰かに役立つことを願っています!!!

于 2012-06-27T08:28:44.447 に答える