Home  >  Article  >  Web Front-end  >  Custom function implements the problem of trim function in js that is incompatible between IE7 and IE8_javascript skills

Custom function implements the problem of trim function in js that is incompatible between IE7 and IE8_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:16:09998browse

The example in this article describes how to use custom functions to implement the trim function in js that is incompatible with IE7 and IE8. Share it with everyone for your reference. The specific implementation method is as follows:

<html> 
 <head> 
  <title>test</title> 
  <script type="text/javascript"> 
    String.prototype.trim = function() 
    { 
      return this.replace(/(^\s*)|(\s*$)/g, ""); 
    } 
     
    function check(){ 
      var str = document.getElementById("test").value; 
       
      alert(str.trim()); 
    } 
  </script> 
 </head> 
  
 <body> 
  <center> 
    <input id="test" type="text" /> 
    <input id="but" type="button" value="检验" onclick="check();"/> 
  </center> 
 </body> 
</html> 

This solves the problem that js on IE does not support trim.

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn