所以我有一个textarea
<textarea>
Very long content. Very long content. Very long content.
Very long content. Very long content. Very long content.
Very long content. Very long content. Very long content.
Very long content. Very long content. Very long content.
Very long content. Very long content. Very long content.
Very long content. Very long content. Very long content.
</textarea>
我想让textarea
适合用户在文本区域中键入的文本,代码如下
const textarea = document.querySelector('textarea')
textarea.addEventListener('keyup', () => {
textarea.style.height = `${textarea.scrollHeight}px`;
})
当用户向文本区域添加更多文本时,它工作正常。问题是当用户清除内部文本并重新开始键入时,文本区域不会缩小以容纳文本。
有办法解决吗?
2条答案
按热度按时间mzsu5hc01#
试试这个,当值为空时它应该收缩文本区域:
iklwldmw2#
用这种方法,你可以达到你想要的。
您还可以使用
span
创建具有所需结果的textarea
。超文本:
CSS: