實時統計輸入字符數

實時統計輸入字符數
 
<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>
如圖所示
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章