This article analyzes the solution that JavaScript cannot use trim function under IE, which has certain reference value for the design of the front part of the web. The specific analysis is as follows:
First of all, there is no problem using the trim function of javascript under firefox:
<script language="javascript"> var test1 = " aa "; test1 = test1.toString(); test1 = test1.trim(); </script>
There is no problem using this under Firefox, but an error is reported under IE!
For this, we can modify it:
String.prototype.trim=function(){return this.replace(/(^/s*)|(/s*$)/g,"");}Add this sentence to the head, and the above can be run under IE and FF:
<script language="javascript"> String.prototype.trim=function(){return this.replace(/(^/s*)|(/s*$)/g,"");} var test1 = " aa "; test1 = test1.toString(); test1 = test1.trim(); </script>Methods provided by JQuery:
<!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>I believe that the description in this article has good reference value for everyone's compatibility design of WEB front-end browsers using javascript.