Demo
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>字数限制</title> </head> <body> <textarea id="b" rows="4" cols="20" ></textarea> <div><span id="a">已输入字符: </span><input type="button" value="OK" onclick="cha()"></div> <script language="javascript"> var maxl=3//总长 document.onkeydown=function(){ var s=document.getElementById("b").value.length +1; if(s>maxl)document.getElementById("b").value=document.getElementById("b").value.substr(0,maxl-1) else document.getElementById("a").innerHTML="已输入:"+s+"/"+maxl+" 字符" } </script> </body> </html>