简体   繁体   English

根据字符串长度动态设置textarea高度

[英]Dynamic set textarea height based on string length

I have a piece of code which I can't solve the part this.scrollHeight... . 我有一段代码无法解决this.scrollHeight...的部分。 The data.address could be a short or long string. data.address可以是短字符串或长字符串。 So my intention is to dynamic set the height of the textarea to fully display the text within but I can't figure out how to do that using code below. 因此,我的目的是动态设置textarea的高度,以完全显示其中的文本,但是我不知道如何使用下面的代码来做到这一点。

$('#myAddress').val(data.address).css('height', this.scrollHeight + this.offsetHeight - this.clientHeight);

Simply set it to scrollHeight. 只需将其设置为scrollHeight。

$("#myAddress").height( $("#myAddress")[0].scrollHeight );

DEMO on JSFiddle 在JSFiddle上进行演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM