ie8兼容:对象不支持“trim”属性或方法

trim() 方法是原生js中去空格的方法,高版本浏览器已经默认支持trim() 方法,但ie8以下不支持,会报错:对象不支持“trim”属性或方法

解决这个的兼容,只需要扩展String原型属性

在公共js中写一个方法

String.prototype.trim = function() {
    return this.replace(/(^s*)|(s*$)/g, ""); //正则匹配空格  
}

也可以用jquery 写函数

 function trim(str){  
    return str.replace(/(^s*)|(s*$)/g, ""); 
}
原文地址:https://www.cnblogs.com/leiting/p/9517547.html