jQuery实现textarea高度自适应

js代码

$.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();

HTML

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

加上

autoHeight="true"

这一段即可

posted @ 2022-08-28 20:28  蚂蚁追风筝  阅读(81)  评论(0)    收藏  举报