<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><texta id="t"> <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.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp.lastMsgLabelOp. == "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>} <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> </texta> </div> <br><br> <br>解輸入日文全角時出現的BUG 主要是紅線中間的程式碼。 思路就是中斷日文的輸入狀態。 用如果輸入超出時能忍受彈跳窗的話,就用方案②,否則的話就用方案①。