are text.trim() really can't work in IE8
when i test my web-page in chrome and firefox they work fine. but in IE it does not worked.
i found that
(" .class li"开发者_如何转开发).text().trim()
not worked in IE he give me error that
Object doesn't support this property or method. but in FF and chrome they work fine. are i goes something wrong to handle this.
Try this:
$.trim($(".class li").text());
The reason that it doesn't worked in your case is because the trim method you were calling was wasn't jquery.trim method. It is a method you were calling on a object instance (.text()
returns a string). So some browsers have this method built-in while IE doesn't.
String.trim
is not part of the old language specification, it is a new kid in town. Fortunetely you can easily add this function.
if (typeof String.prototype.trim != "function") {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
Now you can trim any String
you want:
" just do it ".trim()
$(" .class li").text().trim()
If you want to remove both leading and trailing spaces you need to use
replace(/^\s+|\s+$/g, '')
精彩评论