这是我的 contenteditable 元素:
#editor {
width: 200px;
height: 30px;
border: 1px solid red;
overflow: hidden;
}
<div id="editor" contenteditable="true"></div>
我希望它可以根据用户的内容自动调整大小。我尝试监听 keyup
事件:
editor.addEventListener("keyup", function (){
newheight = editor.scrollHeight;
editor.style.height = newheight + "px";
})
当 div 变高时,这可能会起作用,但是当用户删除所有内容时,div 不能变短。
如何让它自动调整大小?
演示 here
最佳答案
添加“显示:内联 block ;”到你的 CSS 和“min-”到宽度和高度。
您的 DIV 将自动增长 innerHTML 内容。
<html>
<style type="text/css">
#Test
{
display: inline-block;
min-width: 30px;
min-height: 30px;
border: 1px solid red;
overflow: hidden;
}
</style>
<div id="Test" >
Nothing But Bigger
And <br />
Taller
</div>
</html>
关于javascript - 如何自动调整 contenteditable 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19947955/