jQuery这样写:
$(function() {
$.fn.autoHeight = function() {
function autoHeight(elem) {
elem.style.height = 'auto';
elem.scrollTop = 0; //防抖动
elem.style.height = elem.scrollHeight + 2 + 'px';
}
this.each(function() {
autoHeight(this);
$(this).on('input', function() {
autoHeight(this);
});
});
}
$('textarea[autoHeight]').autoHeight();
});
然后在textarea标签中,加入autoHeight
即可。
就像这样
<textarea autoHeight></textarea>
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
作者: Austin 发表日期:2020-11-15 08:38
漂亮!之前我查的网上实现的版本都没办法回缩,你这个完美解决。
新春愉快