作家
登录

JS TextArea字符串长度限制代码集合

作者: 来源:www.28hudong.com 2013-03-30 00:21:21 阅读 我要评论

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

  推荐阅读

  js实现的美女瀑布流效果代码

瀑布流以及回顶部的效果 *{ margin:0; padding:0; } h1{ text-align:center; height:100px; } body{ background-color:RGB(232,231,226); } .all{ margin:0 auto; width:1000px; } .nu>>>详细阅读


本文标题:JS TextArea字符串长度限制代码集合

地址:http://www.17bianji.com/kaifa2/JS/22989.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)