<div class="htmlarea"><textarea id="runcode83643"> JS 计算文本域剩余字数 <textarea cols="20" rows="4" onkeyup="displaySpareNumber(this,10)" onchange="displaySpareNumber(this,10)"> <input id="spareNumber" name="spareNumber" value="10" size="1"> 剩 <br> <br><br> 注: <br> displaySpareNumber(this,10) <br> 与 <br> <input id="spareNumber" name="spareNumber" value="10" size="1"/> </textarea> 中的10是你定义的内容长度. <br><input onclick="runEx('runcode83643')" type="button" value="运行代码"><input onclick="doCopy('runcode83643')" type="button" value="复制代码"> <input onclick="doSave(runcode83643)" type="button" value="保存代码"> <a href="http://www.jb51.net/article/23421.htm" title="查看具体详情" target="_blank">[Ctrl A 全选 注:</a>如需引入外部Js需刷新才能执行</textarea></div>]<script type="text/javascript" language="JavaScript"> function displaySpareNumber(_this,size) { var spareNumber=document.getElementById("spareNumber"); //汉字长度 var len=_this.value.replace(/[^\x00-\xff]/gi,'xx').length; var snum=parseInt(size)-len; spareNumber.value=snum; if(snum<0) { if(_this.value.length!=len) { if((len-_this.value.length)>(size/2)) { _this.value=_this.value.substring(0,size/2); } else { _this.value=_this.value.substring(0,size-(len-_this.value.length)); } } else { _this.value=_this.value.substring(0,size); } spareNumber.value=0; return; } } </script>