[英]trim() function doesn't work in IE8?
每當我在字符串上使用trim()
函數時,它適用於Chrome和Firefox,但我在IE8中收到錯誤說:
Object不支持此屬性或方法
任何人都可以告訴我為什么會發生這種情況,如果有解決方法嗎?
IE8不支持修剪功能。 這是一個polyfill:
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
}
如果你想要你可以添加jquery並使用$ .trim(....)這將工作..
$.trim(" hello ");
給你
"hello"
Internet Explorer僅從版本9開始支持trim()
。
作為參考, String.prototype.trim()
的MDN Polyfill是:
if (!String.prototype.trim) {
(function() {
// Make sure we trim BOM and NBSP
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
String.prototype.trim = function() {
return this.replace(rtrim, '');
};
})();
}
對它的支持是:
+--------+---------+----+-------+--------+
| Chrome | Firefox | IE | Opera | Safari |
+--------+---------+----+-------+--------+
| All | 3.5 | 9 | 10.5 | 5 |
+--------+---------+----+-------+--------+
從那時起,我在@nemo和@ karesh-a的幫助下使用jQuery,我想出了:
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function(){
return jQuery.trim( this );
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.