The trim function of JavaScript is not a problem with Firefox
JS Code
- <script language="JavaScript" >
- var test1 = "AA";
- Test1 = Test1.tostring ();
- Test1 = Test1.trim ();
- </script>
It's not a problem to use under Firefox, but the error is under IE
Then we can modify it.
JS Code
- string.prototype.trim=function () {return this.replace (/(^\s*) | ( \s*$)/g,"");}
Add this sentence to your head, which can be run under IE and ff.
JS Code
- <script language="JavaScript" >
- string.prototype.trim=function () {return this.replace (/(^\s*) | ( \s*$)/g,"");}
- var test1 = "AA";
- Test1 = Test1.tostring ();
- Test1 = Test1.trim ();
- </script>
The method provided by jquery:
HTML code
- <! DOCTYPE HTML>
- <html>
- <head>
- <script src="Http://code.jquery.com/jquery-latest.js"></script>
- </head>
- <body>
- <button>show Trim Example</button>
- <script>
- $ ("button"). Click (function () {
- var str = "Lots of spaces before and after";
- Alert ("'" + str + "'");
- str = Jquery.trim (str);
- Alert ("'" + str + "'-no longer");
- });
- </Script>
- </body>
- </html>
JavaScript cannot be solved with the trim function in IE