textarea输入框随内容撑开高度

Posted gocircle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了textarea输入框随内容撑开高度相关的知识,希望对你有一定的参考价值。

原文链接

 

方法一(jquery):

$(‘textarea‘).each(function () {
  this.setAttribute(‘style‘, ‘height:‘ + (this.scrollHeight) + ‘px;overflow-y:hidden;‘);
}).on(‘input‘, function () {
  this.style.height = ‘auto‘;
  this.style.height = (this.scrollHeight) + ‘px‘;
});

方法二:

function setHeight(element) {
  $(element).css({‘height‘:‘auto‘,‘overflow-y‘:‘hidden‘}).height(element.scrollHeight);
}
$(‘textarea‘).each(function () {
  setHeight(this);
}).on(‘input‘, function () {
  setHeight(this);
});

以上是关于textarea输入框随内容撑开高度的主要内容,如果未能解决你的问题,请参考以下文章