有时,任务是计算在输入框或文本区域中输入的单词数。如果我们想显示多行文本,通常会使用文本区域。当将文本输入到文本区域时,用户可以使用空格作为单词之间或行之间的分隔。本文演示了使用 HTML 和 javascript 代码以及 Jquery 库对输入文本中的单词进行计数的过程。这是通过使用两个不同的示例来说明的。在第一个示例中,对输入的空格或换行符进行计数,以查找字数。在第二个示例中,首先将换行符替换为简单的空格,然后使用文本分割以空格分割文本以查找字数。
示例 1:使用 HTML 和 JavaScript 代码通过计算文本中的空格和换行符来查找单词 Count
代码设计步骤
<!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代码通过文本分割方法查找字数
代码设计步骤
这里单词被分开并放入数组中。
<!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 对文本区域进行字数统计?的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。