<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <script type="text/javascript" src="../js/jquery-1.9.1.min.js"></script> <body> <input type="text" id="nickname" class="nickname" value="" placeholder="請輸入您的名字" onkeyup="limitInputCharLen(this,8);"/> <script type="text/javascript"> function limitInputCharLen(str, maxLen) { var w = 0; var tempCount = 0; //length 獲取字數數,不區分漢子和英文 for(var i = 0; i < str.value.length; i++) { //charCodeAt()獲取字符串中某一個字符的編碼 var c = str.value.charCodeAt(i); //單字節加1 if((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) { w++; } else { w += 2; } if(w > maxLen) { str.value = str.value.substr(0, i); break; } } } </script> </body> </html>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com