首頁  >  文章  >  web前端  >  js計算文字方塊輸入的字元數_javascript技巧

js計算文字方塊輸入的字元數_javascript技巧

WBOY
WBOY原創
2016-05-16 15:35:231183瀏覽

使用JavaScript即時的計算使用者目前輸入的字元數函數代碼:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>JavaScript统计字符数</title> 
<script language="javascript"> 
function CountWords(obj, show_id){ 
 var fullStr = obj.value; 
 var charCount = fullStr.length; 
 var rExp = /[^A-Za-z0-9]/gi; 
 var spacesStr = fullStr.replace(rExp, ' '); 
 var cleanedStr = spacesStr + ' '; 
 do{ 
  var old_str = cleanedStr; 
  cleanedStrcleanedStr = cleanedStr.replace(' ', ' '); 
 }while( old_str != cleanedStr ); 
  var splitString = cleanedStr.split(' '); 
  document.getElementById(show_id).innerHTML=charCount; 
} 
</script> 
</head> 
<body> 
<form> 
<textarea cols="40" rows="5" name="x" onkeyup="CountWords(this,'show')" /> 
</textarea> 
</form> 
<div id="show">0</div> 
</body> 
</html> 

javascript實現統計字元數很簡單,上面分享給大家的程式碼可以直接使用,希望對大家的學習有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn