javascript trim函数在IE下不能用

最近做一个商城购物车模块,写了好久的jquery函数终于实现了各种功能,并且在火狐下完成调试。


最后我在IE8下测试的时候发现有些功能无效,很是郁闷。


在IE下打开脚本调试工具,发现执行到某行代码: span.html().trim()  时报错了;


错误信息为:对象不支持“trim”属性或方法。


呃,很是郁闷,堂堂你微软的IE,居然连这种基础函数都不能支持,简直了~ 有木有


在网上找答案,说的是:trim()方法是原生的js方法,高级浏览器中已经默认支持 trim() ,但ie6、7、8都不支持这个新加的方法。


解决方案是:


1.使用原生js来扩展String方法,String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");}

2. 使用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>



我把解决方案赋值过来,方便以后查阅。


另外一篇博文:


方法1:

使用jquery里面的全局函数$.trim()代替原生js方法trim():
$.trim($("input[type='text']:eq(0)").val().trim());

方法2:

使用原生js来扩展String方法


写成类的方法:[ 调用格式: str.trim(); ]

<script type="text/javascript">  
 
Function.prototype.method = function(name, func) {  
 
  this.prototype[name] = func;  
 
  return this;  
 
};  
 
if(String.prototype.trim){ //判断下浏览器是否自带有trim()方法  
 
String.method('trim', function() {  
 
return this.replace(/^\s+|\s+$/g, '');  
 
});  
 
String.method('ltrim', function() {  
 
return this.replace(/^\s+/g, '');  
 
});  
 
String.method('rtrim', function() {  
 
return this.replace(/\s+$/g, '');  
 
});  
 
}  
 
</script>  


可运行的参照实例:


<input id="demo" type="" value="      左右有空格        " /> 

<a href="javascript:;" οnclick="test();">点击去除空格</a> 

<script type="text/javascript">

   Function.prototype.method = function(name, func) {  
  this.prototype[name] = func;  
  return this;  
};  

if(!String.prototype.trim){ 
//判断下浏览器是否自带有trim()方法  
String.method('trim', function() {  
  return this.replace(/^\s+|\s+$/g, '');  
});  

String.method('ltrim', function() {  
  return this.replace(/^\s+/g, '');  
});  

String.method('rtrim', function() {  
  return this.replace(/\s+$/g, '');  
});  
}  
//测试调用方法: trim()  
document.getElementById("demo").select();  
var str=document.getElementById("demo").value;  
function test(){  
document.getElementById("demo").value=str.trim(); //可换成str.ltrim() 或 str.rtrim()  
document.getElementById("demo").select();  
}  
</script>  




写成函数[ 调用格式: trim(str) ]

<script type="text/javascript">   
 
 function trim(str){ //删除左右两端的空格     
 
     return str.replace(/(^\s*)|(\s*$)/g, "");   
 
   }   
 
  function ltrim(str){ //删除左边的空格   
 
       return str.replace(/(^\s*)/g,"");   
 
   }   
 
   function rtrim(str){ //删除右边的空格   
 
       return str.replace(/(\s*$)/g,"");   
 
  }   
 
</script>   


可运行的参照实例:

<input id="demo" type="" value="      左右有空格        " /> 
<a href="javascript:;" οnclick="test();">点击去除空格</a>

<script type="text/javascript">  
     function trim(str){ //删除左右两端的空格    
         return str.replace(/(^\s*)|(\s*$)/g, "");  
   }  
   function ltrim(str){ //删除左边的空格  
       return str.replace(/(^\s*)/g,"");  
   }  
   function rtrim(str){ //删除右边的空格  
      return str.replace(/(\s*$)/g,"");  
   }  
    //测试调用方法: trim()  
    document.getElementById("demo").select();  
    var str=document.getElementById("demo").value;  
    function test(){  
    document.getElementById("demo").value=trim(str); //可换成str.ltrim() 或 str.rtrim()  
    document.getElementById("demo").select();  
    }  
</script>  <strong>
</strong>



下面是原文连接:


http://blog.aizhet.com/web/8250.html   查看原文

http://www.jb51.net/article/55107.htm  查看原文



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值