Copy codeThe Code is as follows:
<Script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
<Script>
(Function ($ ){
$. Extend ($ ,{
FixedWidth: function (str, length, char ){
Str = str. toString ();
If (! Char) char = "...";
Var num = length-lengthB (str );
If (num <0 ){
Str = substringB (str, length-lengthB (char) + char;
}
Return str;
Function substringB (str, length ){
Var num = 0, len = str. length, tenp = "";
If (len ){
For (var I = 0; I <len; I ++ ){
If (num> length) break;
If (str. charCodeAt (I)> 255 ){
Num + = 2;
Tenp + = str. charAt (I );
} Else {
Num ++;
Tenp + = str. charAt (I );
}
}
Return tenp;
} Else {
Return null;
}
}
Function lengthB (str ){
Var num = 0, len = str. length;
If (len ){
For (var I = 0; I <len; I ++ ){
If (str. charCodeAt (I)> 255 ){
Num + = 2;
} Else {
Num ++;
}
}
Return num;
} Else {
Return 0;
}
}
}
});
}) (JQuery );
Var a = "abcde, who am I? ";
Alert (jQuery. fixedWidth (a, 10 ));
</Script>