jquery 动态调整textarea高度

  将一下代码放在js中    

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

  找到对应的id调用  $('#id').txtaAutoHeight();

posted @ 2020-06-29 16:20  MustangLQT  阅读(552)  评论(0编辑  收藏  举报