<!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="請(qǐng)輸入您的名字" onkeyup="limitInputCharLen(this,8);"/> <script type="text/javascript"> function limitInputCharLen(str, maxLen) { var w = 0; var tempCount = 0; //length 獲取字?jǐn)?shù)數(shù),不區(qū)分漢子和英文 for(var i = 0; i < str.value.length; i++) { //charCodeAt()獲取字符串中某一個(gè)字符的編碼 var c = str.value.charCodeAt(i); //單字節(jié)加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>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com