dpka7974 2016-02-08 21:22
浏览 90
已采纳

在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;
  • 写回答

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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?