function CheckLength(strTemp,maxLength) {
var i, sum, count,charCode;
count = strTemp.length;
sum = 0;
for (i = 0; i < count; i++) {
charCode = strTemp.charCodeAt(i);
if (charCode <= 0x007f) { //127
sum += 1;
}
else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) { //128-2047
sum += 2;
}
else if ((0x0800 <= charCode) && (charCode <= 0xffff)) { //2048-65535
sum += 3;
}
if (sum > maxLength) {
var v = strTemp.substring(0, i);
return v + "...";
}
}
return strTemp;
}
原文链接:http://www.jb51.net/article/40412.htm