zoukankan      html  css  js  c++  java
  • javascript在IE下不能用 trim函数解决方法

    javascript 的trim 函数在firefox 下面使用没有问题

    Js代码  收藏代码
    1. <script language="javascript">  
    2.   var test1 = "    aa    ";  
    3.   test1 = test1.toString();  
    4.   test1 = test1.trim();  
    5. </script>  

    在火狐下这样用没有问题, 但是在IE下就报错
    那么我们可以修改一下

    Js代码  收藏代码
    1. String.prototype.trim=function(){return this.replace(/(^s*)|(s*$)/g,"");}  

     

    在头上加上这一句,上面的就可以在IE和FF下都可以运行了

    Js代码  收藏代码
    1. <script language="javascript">  
    2.   String.prototype.trim=function(){return this.replace(/(^s*)|(s*$)/g,"");}  
    3.   var test1 = "    aa    ";  
    4.   test1 = test1.toString();  
    5.   test1 = test1.trim();  
    6. </script>  
     

    JQuery提供的方法:

    Html代码  收藏代码
    1. <!DOCTYPE html>   
    2. <html>   
    3. <head>   
    4.   <script src="http://code.jquery.com/jquery-latest.js"></script>   
    5. </head>   
    6. <body>   
    7.   <button>Show Trim Example</button>   
    8. <script>   
    9.    
    10. $("button").click(function () {   
    11. var str = "     lots of spaces before and after     ";   
    12. alert("'" + str + "'");   
    13.    
    14. str = jQuery.trim(str);   
    15. alert("'" + str + "' - no longer");   
    16. });   
    17.    
    18. </script>   
    19.    
    20. </body>   
    21. </html>  

     

  • 相关阅读:
    限制结果集行数
    函数的差异
    运算符的差异
    数据类型的差异
    简介
    Spring源码阅读入门指引
    Spring的三种注入方式
    AOP面向切面编程
    leetcode771
    leetcode669
  • 原文地址:https://www.cnblogs.com/ranran/p/3967672.html
Copyright © 2011-2022 走看看