本文共 691 字,大约阅读时间需要 2 分钟。
直接看jquery代码,复制过去即可使用:
使用之前先注册方法
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); }); }); /** * 重置高度 */ function _adjustH(elem){ var $obj = jQuery(elem); return $obj.css({height: $obj.attr('_initAdjustHeight'), 'overflow-y': 'hidden'}).height( elem.scrollHeight ); } }});
然后直接调用即可
$(function(){ $('textarea').autoHeight();});
转载地址:http://bxyl.baihongyu.com/