A function used in the project some time ago, using Javascript to control the textarea height of the text box to scale proportionally as the content grows. Today, I have spent some time changing the implementation method to summarize it. Directly run the Code:
Solution 1:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.1 // EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <HTML> <HEAD> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "> <TITLE> fengzhi» text box textarea highly adaptive growth/scaling </TITLE> <style> textarea {height: 100px; width: 300px ;} </style> </HEAD> <BODY> <! -- In the following code, onpropertychange: IE supported; oninput: FireFox supported; added to be compatible with IE and FF; --> <textarea id = "txtContent" rows = "1" onpropertychange = "ResizeTextarea ()" oninput = "ResizeTextarea ()" onkeyup = "ResizeTextarea () "> Qingfeng production http://jb51.net </textarea> <script type =" text/javascript "> // minimum height var minHeight = 100; // maximum height, if the value is exceeded, the scroll bar var maxHeight = 300 is displayed. function ResizeTextarea () {var t = document. getElementById ('txtcontent '); H = t. scrollHeight; h = h> minHeight? H: minHeight; h = h> maxHeight? MaxHeight: h; t. style. height = h + "px";} script </BODY> </HTML>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Solution 1 in various browsers, the text box is no problem with the adaptive growth of content; however, the contraction of the deleted content is different, and IE and Opera are normal, firefox, Chrome, and Safari will not contract. The reason is that the scrollHeight value is equal to the text box height when the text box content height is smaller than the text box height, rather than the text box content height.
Solution 2:
The Code is as follows:
Fengyunzhi> text box textarea highly adaptive growth/scaling
The Textarea height increases with the Content Self-adaptive and has no scroll bar.
Qingfeng Production
Http://jb51.net </textarea>