在textarea中自动换行 [英] Automatic newline in 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屋!