ie8兼容性问题(endwith,startwith,trim)

ie8浏览器不支持endsWith, trim(),startsWith等方法,在使用中就会遇见兼容性问题

解决方法:

重写上述方法:

1, trim、ltrim 或 rtrim

使用用正则表达式

 String.prototype.trim=function(){
      return this.replace(/(^\s*)|(\s*$)/g, "");
   }
   String.prototype.ltrim=function(){
      return this.replace(/(^\s*)/g,"");
   }
   String.prototype.rtrim=function(){
      return this.replace(/(\s*$)/g,"");
   }

2,StartWith重写

String.prototype.startWith=function(str){
if(str==null||str==""||this.length==0||str.length>this.length)
  return false;
if(this.substr(0,str.length)==str)
  return true;
else
  return false;
return true;
}
//或者使用正则表达式,方法如下
String.prototype.startWith = function(str) {
var reg = new RegExp("^" + str);
return reg.test(this);

3,重写  endsWith

String.prototype.endWith=function(str){
    if(str==null||str==""||this.length==0||str.length>this.length)
        return false;
    if(this.substring(this.length-str.length)==str)
        return true;
    else
        return false;
    return true;
}
//或者使用正则表达式
String.prototype.endWith = function(str) {
var reg = new RegExp(str + "$");
return reg.test(this);
}

注意:

上述方法:均要放入$(function(){

上述方法

});

发布了26 篇原创文章 · 获赞 0 · 访问量 9934

猜你喜欢

转载自blog.csdn.net/weixin_38246518/article/details/79394417