js中trim函数的简单实现

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.   <head>  
  4.     <title>test</title>  
  5.     <script type="text/javascript">  
  6.         String.prototype.trim = function()  
  7.         {  
  8.             return this.replace(/(^s*)|(s*$)/g, "");  
  9.         }  
  10.           
  11.         function check(){  
  12.             var str = document.getElementById("test").value;  
  13.               
  14.             alert(str.trim());  
  15.         }  
  16.     </script>  
  17.   </head>  
  18.     
  19.   <body>  
  20.     <center>  
  21.         <input id="test" type="text" />  
  22.         <input id="but" type="button" value="检验" onclick="check();"/>  
  23.     </center>  
  24.   </body>  
  25. </html>  
原文地址:https://www.cnblogs.com/wnlja/p/4175665.html