jQuery实现textarea高度根据内容自适应

//jQuery实现textarea高度根据内容自适应
$.fn.extend({
    txtaAutoHeight: function () {
        return this.each(function () {
            var $this = $(this);
            if (!$this.attr('initAttrH')) {
                $this.attr('initAttrH', $this.outerHeight());
            }
            setAutoHeight(this).on('input', function () {
                setAutoHeight(this);
            });
        });
        function setAutoHeight(elem) {
            var $obj = $(elem);
            return $obj.css({ height: $obj.attr('initAttrH'), 'overflow-y': 'hidden' }).height(elem.scrollHeight);
        }
    }
});
<textarea name="remark" class="remarkTextarea" id="remark" placeholder="请输入"></textarea>
// textarea高度自适应
$("#remark").txtaAutoHeight();
原文地址:https://www.cnblogs.com/rachelch/p/13426399.html