1 jQuery.fn.extend({
2 autoHeight: function(){
3 return this.each(function(){
4 var $this = jQuery(this);
5 if( !$this.attr('_initAdjustHeight') ){
6 $this.attr('_initAdjustHeight', $this.outerHeight());
7 }
8 _adjustH(this).on('input', function(){
9 _adjustH(this);
10 });
11 });
12 /**
13 * 重置高度
14 * @param {Object} elem
15 */
16 function _adjustH(elem){
17 var $obj = jQuery(elem);
18 return $obj.css({height: $obj.attr('_initAdjustHeight'), 'overflow-y': 'hidden'})
19 .height( elem.scrollHeight );
20 }
21 }
22 });
23 // 使用
24 $(function(){
25 $('textarea').autoHeight();
26 });