textarea文本域高度自适应
在工作中需要文本域在用户输入文字的字数超过文本域定义高度显示进行的整理,以下是代码示例
//定义一个文本域并指定ID(这里隐藏了文本的滑轮)
<textarea id="highlyAdaptive" style="overflow: hidden;" ></textarea>
<script type="text/javascript">
const textarea = document.getElementById('highlyAdaptive');
textarea.addEventListener('input', autoResize);
function autoResize() {
this.style.height = 'auto';
this.style.height = (this.scrollHeight) + 'px';
}
</script>