复制代码 代码如下: 还可以输入140字 <BR> var txtobj = {<BR> divName: "area", //外层容器的class<BR> textareaName: "chackTextarea", //textarea的class<BR> numName: "num", //数字的class<BR> num: 140 //数字的最大数目<BR> }<BR> var textareaFn = function () {<BR> //定义变量<BR> var $onthis; //指向当前<BR> var $divname = txtobj.divName; //外层容器的class<BR> var $textareaName = txtobj.textareaName; //textarea的class<BR> var $numName = txtobj.numName; //数字的class<BR> var $num = txtobj.num; //数字的最大数目<BR> function isChinese(str) { //判断是不是中文<BR> var reCh = /[u00-uff]/;<BR> return !reCh.test(str);<BR> }<BR> function numChange() {<BR> var strlen = 0; //初始定义长度为0<BR> var txtval = $.trim($onthis.val());<BR> for (var i = 0; i < txtval.length; i++) {<BR> if (isChinese(txtval.charAt(i)) == true) {<BR> strlen = strlen + 2; //中文为2个字符<BR> } else {<BR> strlen = strlen + 1; //英文一个字符<BR> }<BR> }<BR> strlen = Math.ceil(strlen / 2); //中英文相加除2取整数<BR> if ($num - strlen < 0) {<BR> $par.html("超出 <b style='color:red;font-weight:lighter' class=" + $numName + ">" + Math.abs($num - strlen) + " 字"); //超出的样式<BR> }<BR> else {<BR> $par.html("还可以输入 <b class=" + $numName + ">" + ($num - strlen) + " 字"); //正常时候<BR> }<BR> $b.html($num - strlen);<BR> }<BR> $("." + $textareaName).live("focus", function () {<BR> $b = $(this).parents("." + $divname).find("." + $numName); //获取当前的数字<BR> $par = $b.parent();<BR> $onthis = $(this); //获取当前的textarea<BR> var setNum = setInterval(numChange, 500);<BR> });<BR> }<BR> textareaFn();<BR>