首页  >  文章  >  web前端  >  如何使用 JavaScript 对文本区域进行字数统计?

如何使用 JavaScript 对文本区域进行字数统计?

PHPz
PHPz转载
2023-09-08 17:09:061809浏览

如何使用 JavaScript 对文本区域进行字数统计?

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

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

代码设计步骤

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

  • 步骤 2 - 制作三个 e388a4556c0f65e1904146cc1a846bee 标签。给定不同的 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 - 制作三个 e388a4556c0f65e1904146cc1a846bee 标签。给定不同的 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 文章中,使用两个不同的示例,给出了如何计算在文本区域中输入的单词数的方法。首先,给出了分析单词之间输入的空格和输入的换行以给出字数的方法。在第二个示例中,在将所有换行符转换为简单空格后,对输入的空格使用文本拆分方法。

以上是如何使用 JavaScript 对文本区域进行字数统计?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除