trim()函数在IE8中不起作用? [英] trim() function doesn't work in IE8?

查看:108
本文介绍了trim()函数在IE8中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在字符串上使用 trim()函数时,它适用于Chrome和Firefox,但我在IE8中收到错误说:

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 support this property or method

任何人都可以告诉我为什么会发生这种情况并且是否有解决方法?

Can anyone tell me why this happens and if there is a work around?

推荐答案

IE8不支持修剪功能。
这是一个polyfill:

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, ''); 
  };
}

这篇关于trim()函数在IE8中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆