简体   繁体   English

jQuery Object不支持IE中的属性或方法trim()

[英]jQuery Object doesn't support property or method trim() in IE

What's up with the jQuery trim method?? jQuery trim方法有什么用?

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

Object doesn't support property or method 'trim' 对象不支持属性或方法'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. 我在FireFox或Chrome中没有这些问题......只有IE 9.0。 Is there something special about trim() ... I didn't get the memo . trim()有什么特别之处......我没有收到备忘录。

IE doesn't have a string.trim() method. IE没有string.trim()方法。

Instead, you can call jQuery's $.trim(str) . 相反,你可以调用jQuery的$.trim(str)

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, '');
}

trim() is not invoked like that in a jQuery context. trim()不像jQuery上下文中那样被调用。

Once you call attr() , that's the end of jQuery chaining. 一旦你调用attr() ,这就是jQuery链接的结束。 See http://api.jquery.com/attr/ http://api.jquery.com/attr/

To do this, do: 为此,请执行以下操作:

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

See: http://api.jquery.com/jQuery.trim/ 请参阅: http//api.jquery.com/jQuery.trim/

Same problem here with IE not having the trim() method. IE没有trim()方法也存在同样的问题。 I solved by adding the trim() if it doesn't exist. 我解决了添加trim()如果它不存在。

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

Works great. 效果很好。

This doesn't have anything to do with jquery. 这与jquery没有任何关系。 Attr is returning a string. Attr正在返回一个字符串。 What it means is that IE doesn't have a trim method on string. 这意味着IE在字符串上没有trim方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM