jQuery 对象不支持 IE 中的属性或方法 trim()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7719508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Object doesn't support property or method trim() in IE
提问by Pablo
What's up with the jQuery trim method??
jQuery 修剪方法是怎么回事?
jQuery('#Reminders').attr('value').trim()
Object doesn't support property or method 'trim'
对象不支持属性或方法“修剪”
jQuery('#Reminders').attr('value')
"5,1,1"
“5,1,1”
$('#Reminders').attr('value').split(',')
[5,1,1]
[0]: "5"
[1]: "1"
[2]: "1"
I don't have these woes in FireFox or Chrome ... only IE 9.0. Is there something special about trim() ... I didn't get the memo .
我在 FireFox 或 Chrome 中没有这些问题……只有 IE 9.0。trim() 有什么特别之处吗...我没有收到备忘录。
回答by SLaks
IE doesn't have a string.trim()
method.
IE 没有string.trim()
方法。
Instead, you can call jQuery's $.trim(str)
.
相反,您可以调用 jQuery 的$.trim(str)
.
回答by Leonardo Montenegro
You can add trim() method on String object, for browsers without support for this method (IE alike).
您可以在 String 对象上添加 trim() 方法,对于不支持此方法的浏览器(IE 类似)。
Simply add these lines before you call trim() method:
只需在调用 trim() 方法之前添加这些行:
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
回答by artlung
trim()
is not invoked like that in a jQuery context.
trim()
在 jQuery 上下文中不会像那样调用。
Once you call attr()
, that's the end of jQuery chaining. See http://api.jquery.com/attr/
一旦调用attr()
,jQuery 链接就结束了。见http://api.jquery.com/attr/
To do this, do:
为此,请执行以下操作:
jQuery.trim(jQuery('#Reminders').attr('value'));
回答by Mauro Ciancio
Same problem here with IE not having the trim()
method. I solved by adding the trim()
if it doesn't exist.
IE 没有该trim()
方法也有同样的问题。我通过添加trim()
如果它不存在来解决。
(function(str) {
if (typeof(str.prototype.trim) === 'undefined') {
str.prototype.trim = function() {
return $.trim(this);
};
}
})(String);
Works great.
效果很好。
回答by aquinas
This doesn't have anything to do with jquery. Attr is returning a string. What it means is that IE doesn't have a trim method on string.
这与 jquery 没有任何关系。Attr 正在返回一个字符串。这意味着 IE 没有对字符串的修剪方法。