On the Internet, I found that the method is simple to implement highly adaptive text box, so I encapsulated this function to be fully compatible with IE6 + and modern browsers! The specific function code is as follows:
/*** Adaptive height of the text box based on the input content * @ authortang bin * @ version0.3 * @ see http://www.phpernote.com/article-9-32.html * @ Param {HTMLElement} input box element * @ param {Number} sets the distance between the cursor and the input box (20 by default) * @ param {Number} sets the maximum height (optional) */var autoTextarea = function (elem, extra, maxHeight) {extra = extra | 20; var isFirefox = !! Document. getBoxObjectFor | 'initnerscreenx' in window, isOpera = !! Window. opera &&!! Window. opera. toString (). indexOf ('Opera '), addEvent = function (type, callback) {elem. addEventListener? Elem. addEventListener (type, callback, false): elem. attachEvent ('on' + type, callback) ;}, getStyle = elem. currentStyle? Function (name) {var val = elem. currentStyle [name]; if (name = 'height' & val. search (/px/I )! = 1) {var rect = elem. getBoundingClientRect (); return rect. bottom-rect. top-parseFloat (getStyle ('paddingtop ')-parseFloat (getStyle ('paddingbottom') + 'px' ;}; return val ;}: function (name) {return getComputedStyle (elem, null) [name] ;}, minHeight = parseFloat (getStyle ('height'); elem. style. maxHeight = elem. style. resize = 'none'; var change = function () {var scrollTop, height, padding = 0, style = ele M. style; if (elem. _ length = elem. value. length) return; elem. _ length = elem. value. length; if (! IsFirefox &&! IsOpera) {padding = parseInt (getStyle ('paddingtop ') + parseInt (getStyle ('paddingbottom');}; scrollTop = document. body. scrollTop | document.doc umentElement. scrollTop; elem. style. height = minHeight + 'px '; if (elem. scrollHeight> minHeight) {if (maxHeight & elem. scrollHeight> maxHeight) {height = maxHeight-padding; style. overflowY = 'auto';} else {height = elem. scrollHeight-padding; style. overflowY = 'den den ';}; style. height = height + extra + 'px '; scrollTop + = parseInt (style. height)-elem. currHeight; document. body. scrollTop = scrollTop;document.doc umentElement. scrollTop = scrollTop; elem. currHeight = parseInt (style. height) ;};}; addEvent ('propertychang', change); addEvent ('input', change); addEvent ('focal ', change); change ();};
The call method is as follows (textarea_id is the id value of textarea ):
autoTextarea(document.getElementById('textarea_id'));
Another simple implementation method:
<Html> Articles you may be interested in
- Javascript restricts that only numbers (including decimal places) can be entered in the text box)
- Display text box effects of line numbers, compatible with browsers such as ie and Firefox
- Completely solves the adaptive width problem of the Button in IE6 and 7.
- Jump to the corresponding Email homepage Based on the Email entered by the user
- A solution that does not work when textarea submits content
- Code used to test the password strength of a user input using Javascript
- Js address bar special effects (display the size of all images with links on the page and view the height of the current browser)
- How to Use zlib to compress output content to speed up web page opening