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();
错误显示:
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屋!
查看全文