`
冽豹之姿
  • 浏览: 40307 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JS实现即时统计文本区域字数

    博客分类:
  • 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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics