Home > Web Front-end > JS Tutorial > body text

Javascript implements word count_javascript skills

WBOY
Release: 2016-05-16 15:51:45
Original
1156 people have browsed it

The word count function works by adding an onKeyup event to the textarea. The event reads the textarea content and obtains the length, and assigns it to the text node that counts the word count. One thing to note here is that adding onKeypress and onKeydown events can also achieve the effect. , but they all have some shortcomings and can cause misunderstandings in some cases. I have tried them all and feel that using only one onKeyup event is the wisest choice.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试文件</title>
<script>
function cal_words(){
  var length = document.getElementById("test").value.length;
  document.getElementById("num").innerHTML = length;
}
</script>
</head>

<body>
<div class="reply">
 <textarea id="test" onKeyUp="cal_words()"></textarea>
 <div>字数:<span id="num">0</span></div>
</div>
</body>
</html>

Copy after login

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!