<strong>limit.js代码 <br></strong><div class="codetitle"> <span><a style="CURSOR: pointer" data="76765" class="copybut" id="copybut76765" onclick="doCopy('code76765')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code76765"> <br>//txt:文本框jquery对象 <br>//limit:限制的字数 <br>//isbyte:true:视limit为字节数;false:视limit为字符数 <br>//cb:回调函数,参数为可输入的字数 <br>function InitLimit(txt,limit,isbyte,cb){ <br>txt.keyup(function(){ <br>var str=txt.val(); <br>var charLen; <br>var byteLen=0; <br>if(isbyte){//原文博客:blog.csdn.net/bluceyoung <br>for(var i=0;i<str.length>if(str.charCodeAt(i)>255){ <br>byteLen+=2; <br>}else{ <br>byteLen++; <br>} <br>} <br>charLen = Math.floor((limit-byteLen)/2); <br>}else{ <br>byteLen=str.length; <br>charLen=limit-byteLen; <br>} <br>cb(charLen); <br>}); <br>} <br></str.length> </div> <br><strong>页面代码</strong>: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="68479" class="copybut" id="copybut68479" onclick="doCopy('code68479')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code68479"> <br> <br> <br><br> <br><meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <br><script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"> <BR></script> <br><br><script type="text/javascript" src="limit.js"></script> <br><script type="text/javascript"> <BR>$(document).ready(function(){ <BR>InitLimit($("#txt"),10,true,function(c){ <BR>if(c>=0){ <BR>$("#show").val("还能输入"+c+"个字"); <BR>}else{ <BR>$("#show").val("已经超过"+(-c)+"个字"); <BR>} <BR>}); <br><br>InitLimit($("#txt1"),10,true,function(c){ <BR>if(c>=0){ <BR>$("#show1").val("还能输入"+c+"个字"); <BR>}else{ <BR>$("#show1").val("已经超过"+(-c)+"个字"); <BR>} <BR>}); <BR>}); <BR></script> <br> <br><br> <br><input type="text" id="txt"><input id="show" type="text"><br> <br><input type="text" id="txt1"><input id="show1" type="text"> <br><br> <br> <br> </div> <br><img src="http://files.jb51.net/file_images/article/201302/2013222155827005.jpg?2013122155859" alt="Jquery实现仿新浪微博获取文本框能输入的字数代码_jquery" >