Home  >  Article  >  Web Front-end  >  jquery implements textarea highly adaptive_jquery

jquery implements textarea highly adaptive_jquery

WBOY
WBOYOriginal
2016-05-16 16:10:171213browse

I shared with you before that I used Javascript to control the textarea height to adaptively grow and shrink with the content. I spent some time today to change the implementation method and summarize it

Copy code The code is as follows:

jQuery.fn.extend({
               autoHeight: function(){
                  return this.each(function(){
                    var $this = jQuery(this);
If( !$this.attr('_initAdjustHeight') ){
$this.attr('_initAdjustHeight', $this.outerHeight());
                 }
                          _adjustH(this).on('input', function(){
                         _adjustH(this);
                     });
                });
                     /**
*Reset altitude
                        * @param {Object} elem
                                 */
function _adjustH(elem){
                      var $obj = jQuery(elem);
                         return $obj.css({height: $obj.attr('_initAdjustHeight'), 'overflow-y': 'hidden'})
.height(elem.scrollHeight);
                }
            }
        });
​​​​ //Use
         $(function(){
               $('textarea').autoHeight();
        });

The above is all the content described in this article. I hope it will be helpful to everyone learning jQuery.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn