2017-02-03 42 views
2

不知道为什么,我得到这个错误:错误:对象不支持属性或方法 'trimLeft'

Object doesn't support property or method 'trimLeft' when browse with IE

我的代码是:

var checkTrimLeadingWhiteSpace = function(str) { 
    if (str && ignoreLeadingWS) { 
     return str.trimLeft(); 
    } 

    return str; 
}; 
+1

查看https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/String/TrimLeft –

回答

1

由于MDN暗示trimLeft函数是非标准的,应该避免没有回退。

但是,你可以这样写:

var checkTrimLeadingWhiteSpace = function(str) { 
    if (str && ignoreLeadingWS) { 
    return str.replace(/^\s+/, ""); 
    } 
    return str; 
}; 

replace(/^\s+/, "")将删除所有空格的字符串的开头。

0

trimLeft() : (Non-standard) This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

的IE 不支持trimLeft()方法也不应该使用非标准功能,正如官方文档所述。您可能会搜索Left Trim in Javascript

希望这会有所帮助。

Source

相关问题