jquery實現textarea 高度自適應
更新時間:2015年03月11日 08:55:45 投稿:hebedich
用jquery實現的textarea 高度自適應代碼。這個動畫效果比較流暢。適合新手學習。非常簡單實用,這里推薦給小伙伴們。
之前給大家分享過用Javascript控制文本框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();
});
以上就是本文所述的全部內容了,希望對大家學習jQuery能夠有所幫助。
相關文章
jQuery Validation Plugin驗證插件手動驗證
jquery.validate是jquery旗下的一個驗證框架,借助jquery的優(yōu)勢,我們可以迅速驗證一些常見的輸入,并且可以自己擴充自己的驗證方法,并且對國際化也有很好的支持,接下來通過本文給大家介紹jQuery Validation Plugin驗證插件手動驗證2016-01-01