首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JQuery AutoGrow插件在AJAX函数后无效

JQuery AutoGrow插件在AJAX函数后无效
EN

Stack Overflow用户
提问于 2011-12-27 23:56:03
回答 1查看 1.5K关注 0票数 1

jQuery自动增长插件扩展文本区以适应其内容。函数如下所示。

代码语言:javascript
复制
(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 load函数之后,我们加载一个新的文本区。因此我触发了这个。

代码语言:javascript
复制
$('.commentslogic').load(window.location.href + ' .commentslogic .inner', function(){
$('textarea').autogrow();
}); 

但它不适用于新的文本区域,而且在FireBug中没有错误报告。帮助!

小提琴迪迪小提琴dum http://jsfiddle.net/JTmND/8/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-28 00:04:47

正如评论中指出的,这个小提琴就是解决方案:http://jsfiddle.net/JTmND/11/

代码语言:javascript
复制
$(document).ready(function() {
    $('textarea').autogrow();

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

如何将新的文本区域添加到dom中(手动或通过ajax回调方法)并不重要,有必要使用jquery选择器来分配自动增长功能。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8646564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档