$(function(){ $.fn.autoHeight = function(){ function autoHeight(elem){ elem.style.height = 'auto'; elem.scrollTop = 0; //防抖動 elem.style.height = elem. ...
$(function(){
$.fn.autoHeight = function(){
function autoHeight(elem){
elem.style.height = 'auto';
elem.scrollTop = 0; //防抖動
elem.style.height = elem.scrollHeight + 'px';
}
this.each(function(){
autoHeight(this);
$(this).on('keyup', function(){
autoHeight(this);
});
});
}
$('textarea[autoHeight]').autoHeight();
})
頁面中的textarea直接加屬性就行
<textarea autoHeight="true" readonly="readonly" > </textarea>
pc 移動端都經過測試,沒問題 放心用吧!