1

jQuery autogrowプラグインは、コンテンツに合わせてtextareaを拡張します。機能は以下の通りです。

(function($) {
    /*
     * Auto-growing textareas; technique ripped from Facebook
     */
    $.fn.autogrow = function(options) {

        this.filter('textarea').each(function() {

            var $this       = $(this),
                minHeight   = $this.height(),
                lineHeight  = $this.css('lineHeight');

            var shadow = $('<div></div>').css({
                position:   'absolute',
                top:        -10000,
                left:       -10000,
                width:      $(this).width(),
                fontSize:   $this.css('fontSize'),
                fontFamily: $this.css('fontFamily'),
                lineHeight: $this.css('lineHeight'),
                resize:     'none'
            }).appendTo(document.body);

            var update = function() {

                var val = this.value.replace(/</g, '&lt;')
                                    .replace(/>/g, '&gt;')
                                    .replace(/&/g, '&amp;')
                                    .replace(/\n/g, '<br/>');

                shadow.html(val);
                $(this).css('height', Math.max(shadow.height() + 20, minHeight));
            }

            $(this).change(update).keyup(update).keydown(update);

            update.apply(this);

        });

        return this;

    }

})(jQuery);

その後、トリガーされ$('textarea').autogrow();ます。JQueryロード関数の後、新しいテキストエリアにロードします。したがって、私はこれをトリガーしました。

$('.commentslogic').load(window.location.href + ' .commentslogic .inner', function(){
$('textarea').autogrow();
}); 

ただし、新しいテキストエリアには適用されません。さらに、FireBugでエラーが報告されることはありません。ヘルプ!

Fiddle dee dee Fiddle dee dum http://jsfiddle.net/JTmND/8/

4

1 に答える 1

2

コメントで理解されているように、このフィドルは解決策です:http: //jsfiddle.net/JTmND/11/

$(document).ready(function() {
    $('textarea').autogrow();

    $('.button').click(function() {
        $('.test').html('<textarea></textarea>');
        $('.test').find('textarea').autogrow();
    });
});

新しいtextareasがどのようにdomに追加されるか(手動またはajaxコールバックメソッドによって)は関係ありません。自動拡張機能を割り当てるには、jqueryセレクターを使用する必要があります。

于 2011-12-27T16:04:47.043 に答える