<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" >