【发布时间】:2016-04-22 13:45:01
【问题描述】:
我有 jQuery 代码,当用户在其中键入时,我可以使用它来自动调整 textarea 的大小。有没有办法让它在到达屏幕底部时停止?
<html>
<head>
<title>Page Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
</head>
<style>
.modalTextArea{
overflow: hidden;
z-index: 1;
}
</style>
<body>
<div class="modal-content">
<textarea class='modalTextArea' rows = 2 style='width: 100%'></textarea>
</div>
<script src = 'js/script.js'></script>
</body>
</html>
$('.modalTextArea').on('keyup', function(e){
$(this).css('height', 'auto');
$(this).height(this.scrollHeight + 10);
});
【问题讨论】:
-
仅供参考,您的
<style>元素应该在<head>内 -
每次用户按键时它都会增长,这很奇怪
-
它只会在滚动高度发生变化时增长,即当按下 Enter 或文本移动到新行时
-
@gurvinder372 - 分配固定值的唯一方法是什么?有什么方法可以动态地找出屏幕的结束位置并让它停在那里?抱歉,如果这听起来很疯狂,我对此一无所知,我只是想弄清楚什么可以做,什么不能做。谢谢
标签: javascript jquery