注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 51CTO三周年庆典之IT北斗..
 帮助

实时统计输入字符数


2007-12-16 17:08:49
 标签:实时 统计 字符   [推送到技术圈]

实时统计输入字符数
 
<script>
    
var $E = document.getElementById;
String.prototype.lenB
= function(){
    
return this.unHtmlReplace().replace(/\*/g," ").replace(/[^\x00-\xff]/g,"**").length;
}

String.prototype.unHtmlReplace
= function () {
    
var s = (this).replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&nbsp;/g," ").replace(/&quot;/g,"\"");
      return s.replace(/&#(\d{2});/g,function($0,$1) {return unescape(
"%"+parseInt($1).toString(16));});
}
      function onModChange(s, max, o) {      //实时显示输入字数的
      if(document.readyState !=
"complete") {
          s = escape(s);
          setTimeout('onModChange(unescape(
"'+ s+ '"),'+ max+ ',$E("'+ o.id+ '"))', 10);
          return;
      }
      var words=s.lenB();
      if(words > max)
          words =
"<font color=red>"+ words+ "</font>";
      else
          words =
"<font color=green>"+ words+ "</font>";
      o.innerHTML =
"已输入"+ words+ "个字符(最多"+ max+ "个字符)";
}
</script>

<!--S 发表评论-->
<div>
    
<label><span>发表评论:</span>
<textarea name="content" id="cmt_content" cols="50" rows="3" onpropertychange="onModChange(this.value, 150, $E('post_status'))"></textarea>
    
</label>
    
<div><em id="post_status">已输入<font color=green>0</font>个字符(最多150个字符)</em></div>
</div>
如图所示




    文章评论
 
2007-12-16 21:25:20
图片看不到

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: