查找 html 文本区域中的行数
我正在编写一个移动网络应用程序,其中滚动条不会显示在设备的浏览器上。因此,我试图动态修改文本区域的高度以使其更大,但是我不知道有什么方法可以实际获取 html 文本区域的行数。任何帮助将不胜感激!
编辑
所以我现在意识到它本身不是换行符,而是实际的换行。因此,当一行完成时,它会将文本换行到下一行。看起来好像是一条新线。有什么办法可以统计这些的数量吗?谢谢!
I'm writing a mobile web application where scrollbars are not displayed on the device's browser. Due to this, I'm trying to dynamically modify the height of the textarea to make it bigger, however I don't know of any way to actually get the line count on an html textarea. Any help would be greatly appreciated!
EDIT
So I realize now that it's not newlines per se, but actual line wrapping. So when one line finishes it wraps the text to the next line. It appears as if it is a new line. Any way to count the number of these? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
文本区域中的行数为
The number of lines in the textarea would be
我创建了一个插件来处理
BitBucket 上的代码
示例使用
工作演示
I have created a plugin to handle line counting and wrap detection in a
<textarea>
.I hope someone can use it.
Code on BitBucket
Sample Usage
Working Demonstration
这是一种计算文本区域中行数(包括换行数)的高效且准确的方法。
此处演示
This is an efficient and accurate method to count the number of lines in a text area, including wrapped lines.
Demo here
我还没有尝试使用本博客中讨论的功能,但您可能会发现它很有用。
http://kirblog.idetalk.com/2010/03 /calculate-cursor-position-in-textarea.html
基本上,如果您创建一个
div
,然后将文本复制到该 div 中,并具有相同的宽度和字体特征,那么您可以获取您需要的信息,例如行数。此示例中的行数很简单,因为如果您知道单行有多少像素高,那么只需找到测试 div 的宽度,您就可以非常准确地了解其中有多少行您的文本区域
。I haven't tried using the function discussed in this blog, but you may find it useful.
http://kirblog.idetalk.com/2010/03/calculating-cursor-position-in-textarea.html
Basically, if you create a
div
and then copy the text into that div, with the same width and font characteristics, you can then get the information you need, such as the number of lines. The number of lines in this example would be easy, in that if you know how many pixels high a single line would be, then just find the width of the test div and you can get a pretty accurate idea as to how many lines are in yourtextarea
.获取 scrollHeight,减去顶部+底部填充,除以 lineHeight。
Get scrollHeight, subtract top+bottom padding, divide by lineHeight.
我很确定没有合理的方法来计算浏览器中显示的行数,特别是考虑到某些浏览器(Safari)允许用户调整文本区域的大小。
这可能很麻烦,但最好的选择可能是根据总字符数除以每行的平均字符数来估计。 :-/
I'm pretty sure there is no reasonable way to count the number of lines as displayed in the browser especially considering some browsers (Safari) allow the user to resize textareas.
It'd be hacky, but your best bet might be to just estimate based on the total characters divided by average number of characters per line. :-/
也许有一种方法可以获得“视觉”行的“原始”数量。您应该读取文本区域的
scrollHeight
属性并将其除以行高。我们来试试吧。从这个 HTML 开始:
然后:
我不确定这是否真的有效,只是一个疯狂的理论。
Maybe there is a way to get the "raw" number of "visual" lines. You should read the
scrollHeight
property of the textarea and divide it by the height of a line. Let's try.Start with this HTML:
Then:
I am not sure if that really works, just a mad theory.
你可以这样计算:
You can calculate is as so:
每行允许的字符数由文本区域的“cols”属性决定。
假设每行 80 个字符,一个好的估计可能是:
不考虑断字和自动换行。
The number of characters allowed per line is dictated by the "cols" attribute of the textarea.
Assuming 80 characters per line, a good estimate may be:
Doesn't account for word-break and word-wrap.