textarea高度自适应,随着内容增加高度增加

$(function(){
        $.fn.autoHeight = function(){    
        function autoHeight(elem){
            elem.style.height = 'auto';
            elem.scrollTop = 0; //防抖动
            elem.style.height = elem.scrollHeight + 'px';
        }
        this.each(function(){
            autoHeight(this);
            $(this).on('keyup', function(){
                autoHeight(this);
            });
        });    
    }                
    $('textarea[autoHeight]').autoHeight();    
})

页面中的textarea直接加属性就行

<textarea  autoHeight="true" readonly="readonly" > </textarea>

pc   移动端都经过测试,没问题 放心用吧!

posted @ 2017-12-20 18:57  一直在路上的猪  阅读(22260)  评论(1编辑  收藏  举报