根据其中有多少文本来扩展文本区域(jquery)
如何让文本区域最初只有 1 行,但当文本到达行末尾时添加另一行,并继续执行此操作,直到最多 5 行?
How can I get a textarea to initially have only 1 row, but then when the text reaches the end of the row to add another row, and keep doing this up until a maximum of 5 rows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
像这样的东西:
http://jsfiddle.net/Q3pPT/
编辑 轻微改进处理换行符:
http://jsfiddle.net/Q3pPT/2/
Something like this:
http://jsfiddle.net/Q3pPT/
edit Slight improvements to handle newlines:
http://jsfiddle.net/Q3pPT/2/
这篇列表部分文章提供了深入的步骤有关如何最好地实现此功能的分步说明。
This A List Part article gives an in-depth, step-by-step description on how to best implement this functionality.
我从 Jake Feasel 的答案中得到了这个想法,它不仅适用于扩展,还适用于收缩文本区域,以防内容较少。
HTML
它还根据单独 div 中的内容显示所需的理想行数
CSS
这是必需的,因为一旦使用此处理程序调整了文本区域的大小,代码就会停止工作
JS
演示: http://jsfiddle.net/roopunk/xPdaP/3/
请注意,它还处理在 之间弹出的滚动条keyDown 和 keyUp。 IMO:这有点烦人。
笔记
希望这有帮助!:)
I took the idea from Jake Feasel's answer and it not only works for expanding but also for contracting the textarea in case there is lesser content.
HTML
It also displays the ideal number of rows required according to the content in a sepatate div
CSS
This is required because, once the textarea has been resized using this handler, the code stops working
JS
Demo: http://jsfiddle.net/roopunk/xPdaP/3/
Notice that it also takes care of the scroll bar which pops up between keyDown and keyUp. IMO: It is kinda bugging.
Note
Hope this helps!:)
检查 Elastic jQuery 插件: http://unwrongest.com/projects/elastic/
您可以使用CSS 上的 max-height 属性与文本区域的 maxlength 相结合,将其限制为 5 行。
Check the Elastic jQuery plugin: http://unwrongest.com/projects/elastic/
You can use the max-height property on your CSS combined with the maxlength of your textarea to limit it to 5 lines.
下载此插件: http://james.padolsey.com/demos/plugins /jQuery/autoresize.jquery.js
download this plugin : http://james.padolsey.com/demos/plugins/jQuery/autoresize.jquery.js