2011-10-10 79 views
52

怎么了jQuery修剪方法?jQuery对象不支持属性或方法修剪()在IE浏览器

jQuery('#Reminders').attr('value').trim() 

对象不支持属性或方法 '修剪'

jQuery('#Reminders').attr('value') 

“5,1,1”

$('#Reminders').attr('value').split(',') 

[5,1,1] 
[0]: "5" 
[1]: "1" 
[2]: "1" 

我没有在Firefox或Chrome,这些不幸...只有IE 9.0。 trim()有什么特别的...我没有得到备忘录。

回答

91

IE没有string.trim()方法。

相反,你可以调用jQuery的$.trim(str)

0

这与jquery没有任何关系。 Attr返回一个字符串。这意味着IE浏览器在字符串上没有修剪方法。

10

可以String对象上添加装饰()方法,针对浏览器不支持此方法(IE一样)。

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

所以你不要覆盖'trim'方法,如果它已经存在,你可以这样做:'String.prototype.trim = String.prototype.trim || function(){ return this.replace(/^\ s + | \ s + $/g,''); }' –

4

同样在这里与不具有trim()方法IE问题:

调用TRIM()方法之前,只需添加这些行。如果它不存在,我通过添加trim()解决。

(function(str) { 
    if (typeof(str.prototype.trim) === 'undefined') { 
     str.prototype.trim = function() { 
      return $.trim(this); 
     }; 
    } 
})(String); 

工程很好。

相关问题