如何使用JavaScript计算文本区域中的字数?


有时,任务是计算输入框或文本区域中输入的字数。如果我们想显示多行文本,通常会使用文本区域。在文本区域中输入文本时,用户可以使用空格作为单词或行之间的分隔符。本文使用HTML和带有Jquery库的javascript代码演示了计算输入文本中字数的过程。这通过使用两个不同的示例来展示。在第一个示例中,计算输入的空格或换行符来查找字数。在第二个示例中,首先将换行符替换为简单的空格,然后使用文本分割方法按空格分割文本以查找字数。

示例1:使用HTML和JavaScript代码通过计算文本中的空格和换行符来查找字数

代码设计步骤

  • 步骤1 − 创建一个HTML文件并开始编写代码。

  • 步骤2 − 创建三个<p>标签。赋予不同的id。一个显示文本,第二个显示字数,第三个显示字符数。

  • 步骤3 − 创建一个函数,并在其中使用for循环开始计算输入文本中的字符。检查是否输入了空格或出现了换行符,然后增加字数的值。

  • 步骤4 − 创建一个按钮,并在单击此按钮时调用上述函数。

  • 步骤5 − 执行程序以验证结果。

<!DOCTYPE html> 
<html> 
<head> 
   <title>Word Count Example</title> 
</head> 
<body> <h1> Count the words written in the text area</h1>
   <h2>Enter the Text </h2>
   <textarea id="text11" rows="4" cols="50"></textarea>
   <button type="button" name="action" onclick="wordcountfunction()">Count Words</button>
   <p  id="paratext" style='white-space:pre'></p>
   <p  id="paracountw"></p>
   <p  id="paracountc"></p>
   <script>
      function wordcountfunction(){
         var x = document.getElementById("paratext");
         var y = document.getElementById("paracountw");
         var z = document.getElementById("paracountc");
         var testString=document.getElementById("text11").value;
         var myArray = testString.replace( /
/g, " " ).split( " " ); if(testString){ x.innerHTML = testString ; }else{ console.log("enter the text in the text area first") } var characterCount = 0; var wordCount = 1; var nn; for (var chr = 0; chr < testString.length; chr++) { nn=testString[chr]; characterCount=characterCount+1; if(nn == ' ' || nn.indexOf("
") != -1){ wordCount++; } y.innerHTML = "word Count : " + wordCount ; z.innerHTML = "CHARACTER count : " + characterCount ; document.getElementById("text11").value = ""; } } </script> </body> </html>

查看结果 - 示例1

要查看结果,请在浏览器中打开html文件。现在单击按钮并检查结果。

示例2:使用HTML和JavaScript代码通过使用文本分割方法来查找字数

代码设计步骤

  • 步骤1 − 创建一个html文件并开始编写代码。

  • 步骤2 − 创建三个<p>标签。赋予不同的id。一个显示文本,第二个显示字数,第三个显示字符数。

  • 步骤3 − 创建一个函数,并在其中检查是否存在任何换行符。将其替换为空格。现在,按空格分割文本并将部分内容存储在数组中。数组中输入的单词数就是字数。

  • 步骤4 − 创建一个按钮,并在单击此按钮时调用上述函数。

  • 步骤5 − 运行程序并显示结果。

这里单词被分隔并放入数组中。

<!DOCTYPE html> 
<html> 
<head> 
   <title>Word Count Example</title> 
</head> 
<body> <h1> Count the words by using text Split</h1>
   <h2>Enter the Text </h2>
   <textarea id="text11" rows="4" cols="50"></textarea>
   <button type="button" name="action" onclick="wordcountfunction()">Count Words</button>
   <p  id="paratext" ></p>
   <p  id="paracountw"></p>
   <p  id="paracountc"></p>
   <script>
      function wordcountfunction(){
         var x = document.getElementById("paratext");
         var y = document.getElementById("paracountw");
         var z = document.getElementById("paracountc");
         var testString=document.getElementById("text11").value;
         var myArray = testString.replace( /
/g, " " ).split( " " ); if(testString){ x.innerHTML = "The words entered: " + testString ; }else{ console.log("enter the text in the text area first") } var characterCount=0; var wordCount=1; var n; for (var i = 0; i < testString.length; i++) { n=testString[i]; characterCount=characterCount+1; if(n == ' ' || n.indexOf("
") !=-1){ wordCount = wordCount+1; } y.innerHTML = "word Count : " + wordCount ; z.innerHTML = "CHARACTER count : " + characterCount ; document.getElementById("text11").value = ""; } } </script> </body> </html>

查看结果

要显示结果,请在浏览器中打开html文件。现在在文本区域中输入一些行。单击“计算字数”按钮以查看字数。

在这篇JavaScript-HTML文章中,通过两个不同的示例,给出了显示如何在文本区域中计算输入字数的方法。首先,给出了分析单词之间输入的空格和输入的换行符以给出字数的方法。在第二个示例中,在将所有换行符转换为简单的空格后,对输入的空格使用文本分割方法。

更新于:2023年5月4日

1K+ 次浏览

启动您的职业生涯

完成课程后获得认证

开始学习
广告
© . All rights reserved.