textarea 自适应高度

时间:2025-04-07 07:54:00
$(document).on('input propertychange', 'textarea', function () { this.rows =1 // 先将 rows 设为1, ‘可以不设置试一试删除文本的时候会不会改变行高’ var text_height = $(this).height() // 获取textarea标签中文字所占高度 var padding_top_and_bottom = $(this).innerHeight() - text_height // 计算padding所占高度 var rows = (this.scrollHeight - padding_top_and_bottom ) / text_height // 计算行数 this.rows = rows // 改变 textarea 的行数 })