網頁中的textarea高度有的時候是需要能自定變化適應需求,才能滿足我們的需要?如何實現呢?下面361源碼在網絡中找到了利用jquery實現textarea 高度自適應的方法,分享給大家。
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); }); }); /** * 重置高度 * @param {Object} elem */ function _adjustH(elem){ var $obj = jQuery(elem); return $obj.css({height: $obj.attr('_initAdjustHeight'), 'overflow-y': 'hidden'}) .height( elem.scrollHeight ); } } }); // 使用 $(function(){ $('textarea').autoHeight(); });
完成,到這里就分享完成了,以上代碼361源碼轉自腳本之家。
以上就是jquery實現textarea 高度自適應的全部內容,希望對大家的學習和解決疑問有所幫助,也希望大家多多支持武林網。新聞熱點
疑難解答
圖片精選