日期:2014-05-16  浏览次数:20602 次

JS实现即时统计文本区域字数
示例源码如下:
以下是引用片段:
<html>
<head>
<script language="javascript">
function textareastrlen(str){ //统计文本区域字数函数 www.DeepTeach.com
var len;
var i;
len = 0;
for (i=0;i<str.length;i++) {
   if (str.charCodeAt(i)>255) {
len+=2;  
       }
      else {
     len++;
       }
}
return len;
}
function countChar(textareaName,spanName){ //显示文本区域字数函数
document.getElementById(spanName).innerHTML="共" + document.getElementById(textareaName).value.length +"个字;" + textareastrlen(document.getElementById(textareaName).value) + "个字节。";
}
</script>
</head>
<body>
<TEXTAREA onkeydown=countChar("Content","Content_Num"); onkeyup=countChar
("Content","Content_Num"); id="Content" name="Content" cols=200 rows=2 style="WIDTH: 240px"></TEXTAREA>
<!--实现countChar("Content","Content_Num")显示-->
<span id="Content_Num" ></span>

</body>
</html>


作者:richyue
来自:it学习网原创

该文章转载自IT学习网:http://www.itstudy.cn/www/articleContent.asp?ID=37