textarea高度随着内容增加而增加
2022-10-26 13:45:15 来源:admin 点击:566
<textarea autoHeight="true">asd</textarea>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
<script>
$(function(){
$.fn.autoHeight = function(){
function autoHeight(elem){
elem.style.height = 'auto';
elem.scrollTop = 0; //防抖动
elem.style.height = elem.scrollHeight+10 + 'px';
}
this.each(function(){
autoHeight(this);
$(this).on('keyup', function(){
autoHeight(this);
});
});
}
$('textarea[autoHeight]').autoHeight();
})
</script>