ie7/8浏览器报错:对象不支持“trim”属性或方法

方法1:

使用jquery里面的全局函数$.trim()代替原生js方法trim():

$.trim( 你要替换的字符 );

方法2:

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
if (!String.prototype.trim) { //判断下浏览器是否自带有trim()方法
    String.method('trim', function () {
        return this.replace(/^\s+|\s+$/g, '');
    });
    String.method('ltrim', function () {
        return this.replace(/^\s+/g, '');
    });
    String.method('rtrim', function () {
        return this.replace(/\s+$/g, '');
    });
}

使用方法2,这样就可以直接在你的js里面 原生调用 .trim()  方法了

猜你喜欢

转载自blog.csdn.net/qq_23994787/article/details/85627594