4
<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>

当 textarea 溢出时,它会得到滚动条和 scrollheight,它们会被应用到它的高度,但不能减少 textarea 的高度,因为在减小它的值长度时它不会得到滚动条

4

2 回答 2

21

textarea 的最小 scrollHeight 始终是高度。要获得准确的滚动高度,请先将高度设置为 1。

h.height(1).height(h[0].scrollHeight);

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

于 2012-05-23T14:48:43.490 回答
0

如果您不想自己编写代码,也可以使用此脚本:https ://github.com/brandonaaron/jquery-expandable 它已经包含一个不错的幻灯片效果 ;-)

于 2012-05-23T15:00:38.423 回答