浏览器兼容性问题汇总

孤人 提交于 2020-07-27 04:00:51

1 IE 浏览器不支持事件和方法的解决方案:

1.1 trim()

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,"");
}

1.2 startsWith()

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);
}

String.prototype.startsWith = function (prefix){  
  return this.slice(0, prefix.length) === prefix;  
};  

1.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);
}

String.prototype.endsWith = function(suffix) {  
  return this.indexOf(suffix, this.length - suffix.length) !== -1;  
}; 

2 谷歌、火狐浏览器不支持事件和方法的解决方案

2.1 onpropertychange

function onpropertychange(){
   var element = document.getElementById("inputID");
   if("/v"=="v") {
      element.onpropertychange = textChange;
   }else{
   	  element.addEventListener("input",textChange,false);
   }
   function textChange(){
 	  alert(element.value);
   }
};

<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
$("#inputID").bind("input propertychange",function(){
   alert($("#inputID").val());
});

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!