日期:2014-05-18 浏览次数:20764 次
<!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=gb2312" /> <title> 无标题文档 </title> </head> <body> <textarea cols="60" rows="10" id="text1"></textarea><br> 剩余字符:<span id="textleft">20</span> </body> </html> <script language="javascript"> var otext = document.getElementById("text1"); var otextleft = document.getElementById("textleft"); var re = /[^\x00-\xff]/g; var maxLength = 20; window.onload = function(){ if(document.all) otext.onpropertychange = textleft; else otext.onkeypress = textleft; } function textleft() { var str = otext.value; str = str.replace(re,"xx");//中文字符当2个字符处理 如果不用的话主是掉本行 otextleft.innerHTML = maxLength - str.length; if((maxLength - str.length)<0){ otext.value=otext.value.substr(0,otext.value.length-1); } }
------解决方案--------------------
<div> <textarea id="TextArea1" cols="20" rows="20" onkeyup="check()"></textarea> <label id="Label1"></label> </div> <script type="text/javascript"> function check() { var textLimit = 200; var otextarea = document.getElementById("TextArea1"); var olabel = document.getElementById("Label1"); var textLength = otextarea.value.length; if(textLength <= textLimit) { olabel.innerHTML = (textLimit - textLength) + "remains"; } else { olabel.innerHTML = (textLength - textLimit) + "overflow" } } </script>