dpka7974
2016-02-08 21:22
浏览 62

在textarea中查找每行有多少个字符

I have a textarea, and I need to figure out how many characters there are per row, in my textarea. The characters per row changes depending on the character size, and the cols of the textarea, so I'm wondering if there is a way to find it without counting them manually.

<textarea cols = '5' rows = '10'>1234567</textarea> 
^----7 characters fit in a textarea with a cols of 5, and default font-size;

<textarea cols = '6' rows = '10'>12345678</textarea> 
^----8 characters fit in a textarea with a cols of 6, and default font-size;

<textarea cols = '7' rows = '10'>123456789</textarea> 
^----9 characters fit in a textarea with a cols of 7, and default font-size;

图片转代码服务由CSDN问答提供 功能建议

我有一个textarea,我需要弄清楚我的textarea中每行有多少个字符。 每行的字符根据字符大小和textarea的cols而变化,所以我想知道是否有办法找到它而不用手动计算它们。

 &lt; textarea cols ='5'rows ='10'&gt; 1234567&lt; / textarea&gt;  
 ^ ---- 7个字符适合textarea,cols为5,默认字体大小; 
 
&lt; textarea cols ='6'rows ='10'&gt; 12345678&lt; / textarea&gt;  
 ^ ---- 8个字符适合textarea,cols为6,默认字体大小; 
 
&lt; textarea cols ='7'rows ='10'&gt; 123456789&lt; / textarea&gt;  
 ^ ---- 9个字符适合textarea,cols为7,默认字体大小; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufan9290 2016-02-09 11:14
    已采纳

    Do check below code. Hope so it will help you. Here "arrayOfLines[i].length" will return total characters in one line. I have written them on console.

    <script type="text/javascript">
        function calculate() {
            var textArea = document.getElementById("my-text-area");
            var arrayOfLines = textArea.value.split("
    ");
            for(var i = 0;i < arrayOfLines.length;i++){
                console.log(arrayOfLines[i].length);
            }
        }
    </script>
    

    Textarea and button to calculate characters

    <textarea id="my-text-area"></textarea>
    <button onclick="calculate();">Calcualte</button>
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题