css 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);
});

猜你喜欢

转载自blog.csdn.net/liuxiao723846/article/details/80890605