JavaScript 中的 .trim() 在 IE 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2308134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.trim() in JavaScript not working in IE
提问by Jin Yong
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?
我试图.trim()在我的一个 JavaScript 程序中应用到一个字符串。它在 Mozilla 下运行良好,但在 IE8 中尝试时显示错误。有谁知道这里发生了什么?无论如何我可以让它在IE中工作吗?
code:
代码:
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
回答by Ben Rowe
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, '');
}
}
回答by jrummell
It looks like that function isn't implemented in IE. If you're using jQuery, you could use $.trim()instead (http://api.jquery.com/jQuery.trim/).
看起来该功能未在 IE 中实现。如果您使用的是 jQuery,则可以$.trim()改用 ( http://api.jquery.com/jQuery.trim/)。
回答by Dan
jQuery:
jQuery:
$.trim( $("#mycomment").val() );
Someone uses $("#mycomment").val().trim();but this will not work on IE.
有人使用,$("#mycomment").val().trim();但这不适用于 IE。
回答by Iain Collins
Unfortunately there is not cross browser JavaScript support for trim().
不幸的是,trim() 没有跨浏览器的 JavaScript 支持。
If you aren't using jQuery (which has a .trim() method) you can use the following methods to add trim support to strings:
如果您不使用 jQuery(它有一个 .trim() 方法),您可以使用以下方法为字符串添加修剪支持:
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}
回答by Erik
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/String/Trim
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/String/Trim
This is a pretty recent addition to javascript, and its not supported by IE.
这是 javascript 的最新添加,IE 不支持。
回答by kaichanvong
I had a similar issue when trying to trim a value from an input and then ask if it was equal to nothing:
我在尝试从输入中修剪一个值然后询问它是否等于空时遇到了类似的问题:
if ($(this).val().trim() == "")
However this threw a spanner in the works for IE6 - 8. Annoyingly enough I'd tried to var it up like so:
然而,这在 IE6 - 8 的工作中投入了一个扳手。 令人讨厌的是,我试图像这样改变它:
var originalValue = $(this).val();
However, using jQuery's trim method, works perfectly for me in all browsers..
但是,使用 jQuery 的 trim 方法,在所有浏览器中都非常适合我。
var originalValueTrimmed = $.trim($(this).val());
if (originalValueTrimmed == "") { ... }
回答by shiv.mymail
I have written some code to implement the trim functionality.
我已经编写了一些代码来实现修剪功能。
LTRIM(trim left):
LTRIM(修剪左):
function ltrim(s)
{
var l=0;
while(l < s.length && s[l] == ' ')
{ l++; }
return s.substring(l, s.length);
}
RTRIM(trim right):
RTRIM(右修剪):
function rtrim(s)
{
var r=s.length -1;
while(r > 0 && s[r] == ' ')
{ r-=1; }
return s.substring(0, r+1);
}
TRIM(trim both sides):
TRIM(修剪两侧):
function trim(s)
{
return rtrim(ltrim(s));
}
OR
或者
Regular expression is also available which we can use.
我们也可以使用正则表达式。
function trimStr(str) {
return str.replace(/^\s+|\s+$/g, '');
}
回答by Will V King
We can get official code From the internet! Refer this:
我们可以从网上获取官方代码!参考这个:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim
Running the following code before any other code will create trim() if it's not natively available.
if (!String.prototype.trim) { (function() { // Make sure we trim BOM and NBSP var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; String.prototype.trim = function() { return this.replace(rtrim, ''); }; })(); }
如果本机不可用,则在任何其他代码之前运行以下代码将创建 trim()。
if (!String.prototype.trim) { (function() { // Make sure we trim BOM and NBSP var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; String.prototype.trim = function() { return this.replace(rtrim, ''); }; })(); }
for more: I just found there is js project for supporting EcmaScript 5: https://github.com/es-shims/es5-shimby reading the source code, we can get more knowledge about trim.
更多:我刚刚发现有一个支持EcmaScript 5的js项目:https: //github.com/es-shims/es5-shim通过阅读源代码,我们可以获得更多关于trim的知识。
defineProperties(StringPrototype, { // http://blog.stevenlevithan.com/archives/faster-trim-javascript // http://perfectionkills.com/whitespace-deviations/ trim: function trim() { if (typeof this === 'undefined' || this === null) { throw new TypeError("can't convert " + this + ' to object'); } return String(this).replace(trimBeginRegexp, '').replace(trimEndRegexp, ''); } }, hasTrimWhitespaceBug);
defineProperties(StringPrototype, { // http://blog.stevenlevithan.com/archives/faster-trim-javascript // http://perfectionkills.com/whitespace-deviations/ trim: function trim() { if (typeof this === 'undefined' || this === null) { throw new TypeError("can't convert " + this + ' to object'); } return String(this).replace(trimBeginRegexp, '').replace(trimEndRegexp, ''); } }, hasTrimWhitespaceBug);
回答by Mark
I had the same problem in IE9 However when I declared the supported html version with the following tag on the first line before the
我在 IE9 中遇到了同样的问题但是当我在第一行声明支持的 html 版本之前
<!DOCTYPE html>
<HTML>
<HEAD>...
.
.
The problem was resolved.
问题得到解决。

