当值增加时,textarea的高度增加,但当值减小时,textarea的高度不减少

3 html javascript jquery

<body>
<script type="text/javascript">$(function(){
$("textarea").live("keyup keydown",function(){var h=$(this);
h.height(h[0].scrollHeight);
});});

</script>
<textarea style="resize:none;width:760px;height:60px; overflow:hidden;" ></textarea>
</body>
Run Code Online (Sandbox Code Playgroud)

当textarea溢出时,它会获得滚动条和滚动高度,它会被应用到它的高度,但不能用于降低textarea高度,因为减小它的值长度它不会得到滚动条

小智 18

textarea的最小scrollHeight总是高度.要获得准确的scrollHeight,请先将高度设置为1.

h.height(1).height(h[0].scrollHeight);
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/aarongloege/t2vAr/