網(wǎng)頁中的textarea高度有的時候是需要能自定變化適應(yīng)需求,才能滿足我們的需要?如何實現(xiàn)呢?下面361源碼在網(wǎng)絡(luò)中找到了利用jquery實現(xiàn)textarea 高度自適應(yīng)的方法,分享給大家。
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源碼轉(zhuǎn)自腳本之家。
以上就是jquery實現(xiàn)textarea 高度自適應(yīng)的全部內(nèi)容,希望對大家的學(xué)習(xí)和解決疑問有所幫助,也希望大家多多支持武林網(wǎng)。新聞熱點
疑難解答
圖片精選