因为text area的高度不能自适应,但是项目又是需要回显文本加又可输入并且不要滚动条的需求,试了很多方法都不行,最终get到:
用div模拟text area去实现高度自适应,并且可输入:
<div class="test_box" contenteditable="true"> </div>
css部分:
.test_box { width: 400px; min-height: 20px; /*设置最小高度*/ max-height: 1000px; /*设置最大高度超过300px时出现滚动条*/ _height: 120px; margin-left: auto; margin-right: auto; padding: 13px; outline: 0; border: 1px solid #a0b3d6; font-size: 16px; line-height: 24px; word-wrap: break-word; overflow-x: hidden; overflow-y: auto; }
contenteditable ='true';是HTML5新属性,规定是否可规定元素的内容,true/false;