JavaScript中的.trim()无法在IE中运行 [英] .trim() in JavaScript not working in IE
本文介绍了JavaScript中的.trim()无法在IE中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试应用 .trim()
到我的一个JavaScript程序中的字符串。它在Mozilla下工作正常,但是当我在IE8中尝试它时会显示错误。有谁知道这里发生了什么?无论如何我可以在IE中使用吗?
I tried to apply .trim()
to a string in one of my JavaScript programs. It's working fine under Mozilla, but an error displays when I try it in IE8. Does anyone know what is going on here? Is there anyway I can make it work in IE?
var ID = document.getElementByID('rep_id').value.trim();
错误显示:
error display:
Message: Object doesn't support this property or method
Line: 604
Char: 2
Code: 0
URI: http://test.localhost/test.js
推荐答案
添加以下代码以向字符串添加修剪功能。
Add the following code to add trim functionality to the string.
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
这篇关于JavaScript中的.trim()无法在IE中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文