<div class="codetitle"> <span><a style="CURSOR: pointer" data="66431" class="copybut" id="copybut66431" onclick="doCopy('code66431')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code66431"> <br> <br> <br><textarea id="t"></textarea> <br><input type="text" id="b2"> <br><script type="text/javascript"> <BR>textAreaLimit("t",{lastMsgLabel:"b2"}) <BR>function textAreaLimit(area,op){ <BR>var defaultOp = { <BR>maxLength:10 //最大长度 <BR>, IsNumber:false //只能是数字 <BR>, lastMsgLabel:null //即时显示可输入个数的Input <BR>, msg:"还可以输入{0}个文字" <BR>, errorMsg:"文字个数超出最大限制" <BR>}; <BR>var label; <BR>if(typeof area == "string"){ <BR>area = document.getElementById(area); <BR>} <BR>if(!area){ <BR>return; <BR>} <BR>for(var i in op){ <BR>defaultOp[i] = op[i]; <BR>} <BR>if(defaultOp.lastMsgLabel){ <BR>if(typeof defaultOp.lastMsgLabel == "string"){ <BR>label = document.getElementById(defaultOp.lastMsgLabel); <BR>} <BR>} <BR>if(defaultOp.IsNumber){ <BR>area.style.imeMode="Disabled";//IE <BR>area.onkeydown = function(){ <BR>return event.keyCode != 229; <BR>} <BR>} <BR>area.onkeyup = function(){ <BR>if(defaultOp.IsNumber){ <BR>this.value = this.value.replace(/\D/g,"");//IE之外的 <BR>} <BR>if(this.value.length > defaultOp.maxLength){ <BR>//------------------------------------------------------------------------------- <BR>//方案① <BR>this.disabled = "disabled"; <BR>this.value = this.value.slice(0,defaultOp.maxLength); <BR>this.removeAttribute("disabled"); <BR>this.focus(); <BR>//方案② <BR>//或 <BR>//alert(defaultOp.errorMsg); <BR>//this.value = this.value.slice(0,defaultOp.maxLength); <BR>//------------------------------------------------------------------------------- <BR>} <BR>if(label){ <BR>label.value = defaultOp.msg.replace(/\{0\}/,defaultOp.maxLength -this.value.length); <BR>} <BR>} <BR>} <BR></script> <br> <br> <br> </div> <br>解决输入日语+全角时出现的BUG 主要是在红线中间的代码。 <br>思路就是中断日语的输入状态。 <br>用如果输入超出时能忍受弹窗的话,就用方案②,否则的话就用方案①。