trim() function doesn't work in IE8?

前端 未结 4 1122
栀梦
栀梦 2020-12-25 11:35

Whenever I use the trim() function on a string, it works fine with Chrome and Firefox but I get an error in IE8 saying :

Object doesn\'t

4条回答
  •  渐次进展
    2020-12-25 12:37

    IE8 doesn't support the trim function. Here's a polyfill:

    if(typeof String.prototype.trim !== 'function') {
      String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, ''); 
      };
    }
    

提交回复
热议问题