在textarea中自动换行 [英] Automatic newline in textarea

查看:841
本文介绍了在textarea中自动换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery,我怎样才能制作一个textarea,当光标靠近结尾时插入文本时会自动添加一行。



textarea的宽度是通过CSS动态完成(例如, #myTextArea {width:80%;} ),所以我无法做任何事情来计算每行中的字符。 b
$ b

我不需要非JS解决方案,因为textarea的内容只有JavaScript才能看到。

解决方案

您可以获得文本区域的计算像素宽度和textarea的填充。你可以查看你字体的平均宽度(或模式,或其他任何你喜欢的)。你可以猜测字距。看看我在做什么? (提示:以字符为单位计算行长)

你提到了数字字符,所以我假设你已经知道如何做到这一点。 :-)


Using jQuery, how can I make a textarea that automatically adds a new line when inserting text when the cursor is near the end.

The width of the textarea is dynamically done via CSS (e.g., #myTextArea { width: 80%; }) so I can't do anything like counting the characters in each line.

I don't need a non-JS solution since the content of the textarea is only ever seen by JavaScript anyways.

解决方案

You can get the calculated pixel width of the text area and the textarea's padding. You can look up the average (or mode, or whatever pleases you) horizontal width of your font. You can make a guess at the kerning. See what I'm getting at? (hint: calculate the line length in characters)

You mentioned counting characters, so I assume you already know how to do that. :-)

这篇关于在textarea中自动换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆