应该是前后有看不到空白字符,不行你自己console.log打印单个字符看就知道了,调试工具不会出错的
function getTextLength(txt) { for(var i=0;i<txt.length;i++) console.log(txt.charAt(i), txt.charCodeAt(i)) return txt.length; }
检查你传txt是不是为空值,不然正常是不会报错的。