2017-02-03 65 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

相關問題