Directly upload the code:
Option 1:
]
Option 1 is available in each browser , there is no problem with the text box adaptively growing with the content; however, there is a difference in shrinking performance when deleting content. IE and Opera behave normally, but Firefox, Chrome, and Safari do not shrink. The reason is that when the text box content height is less than the text box height, the scrollHeight value is equal to the text box height, not the text box content height.
Option 2:
The code is as follows:
;textarea id="txtContent" rows="5" cols="50" onkeyup="ResizeTextarea()" style="overflow-y:hidden;">Textarea height adaptively grows with the content, no scroll bar
Produced by Qingfeng
http://jb51.net
Option 2 in each browser The performance is the same, and the text box can be adaptively grown and shrunk with the content. But one drawback is that when the height of the text box increases, the text box display will jump.
In addition, the above two solutions are invalid for operating text content by selecting cut, paste, delete and other commands from the text box right-click menu. It can be said that a perfect solution has not yet been found, leaving it to be studied in the future. If any students have a perfect plan, please let me know!
Other options:
A different approach to elastic textareas
Solution 2 is implemented by referring to this article
-
Build an Elastic Textarea with Ext JS
-
Smart TextArea: scrollHeight in IE, Firefox, Opera and Safari
-
Original link: http://witmax.cn/javascript-textarea-auto-grow.html