我正在使用trim(),如下所示:
if($('#group_field').val().trim()!=''){
其中group_field
是文本类型的输入元素。这在火狐中是有效的,但当我在IE8上尝试它时,它给出了这个错误:
Message: Object doesn't support this property or method
当我删除trim()时,它在IE8上工作得很好。我想我使用trim()的方式是正确的吗?
感谢所有人的帮助
发布于 2010-08-09 18:58:35
您应该使用$.trim
,如下所示:
if($.trim($('#group_field').val()) !='') {
// ...
}
发布于 2010-08-09 19:01:12
据我所知,Javascript String没有trim方法。如果要使用函数trim,则使用
<script>
$.trim(string);
</script>
发布于 2011-12-09 17:52:03
另一种选择是直接在String
上定义方法,以防它丢失:
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
//Your implementation here. Might be worth looking at perf comparison at
//http://blog.stevenlevithan.com/archives/faster-trim-javascript
//
//The most common one is perhaps this:
return this.replace(/^\s+|\s+$/g, '');
}
}
然后,无论使用哪种浏览器,trim
都将正常工作:
var result = " trim me ".trim();
https://stackoverflow.com/questions/3439316
复制相似问题