简体   繁体   English

在段落换行到下一行之前如何获取字符数?

[英]How to get count of characters before paragraph wraps into next line?

I'm trying to count how many characters are displayed in the first line of a text which is wrapped because the width of the span is limited. 我试图计算由于跨度的宽度有限而在换行的文本的第一行中显示了多少个字符。

I know that counting the characters of a String can be done with "length", but I'm failing to get the first line. 我知道可以用“长度”来完成对字符串字符的计数,但是我没有得到第一行。

Example: 例:

 #divone { width: 80px; overflow: hidden; } 
 <div id="divone"> <p id="one">Some long text <p> </div> 

Displayed the text appears as first line: "some long" and second line "text" 显示的文本显示为第一行:“ some long”和第二行“ text”

Expected output from JavaScript: JavaScript的预期输出:

Some long

Pseudocode I thought to solve it: 我想解决的伪代码:

  1. Detect which is the last word before the text is wrapped 在换行之前检测哪个是最后一个单词

  2. Look this word up in original text and retrieve a substring 在原始文本中查找此单词并检索一个子字符串

  3. Use length() to count characters of the substring. 使用length()计算子字符串的字符。

Thanks to comments I found I solution which is approx. 由于评论,我发现我的解决方案是大约。 what I want: 我想要的是:

How to count number of characters in a line using JavaScript 如何使用JavaScript计算一行中的字符数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM