textarea高度自适应,随着内容增加高度增加

it2022-05-05  98

AutoHeightTextArea() { $.fn.autoHeight = function() { function autoHeight(elem) { var height = $('#height').height() console.log(height) elem.style.height = height + 'px' if(elem.scrollHeight > 0){ // 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标签上增加属性 autoHeight = “true” 即可。

引用:https://www.cnblogs.com/purple04551/p/8075366.html


最新回复(0)