problèmes de compatibilité ie8 (endwith, startwith, trim)

Le navigateur ie8 ne prend pas en charge les méthodes de endsWith, trim (), startsWith, etc.

Solution:

Réécrivez la méthode ci-dessus:

1, trim, ltrim ou rtrim

Utilisez des expressions régulières

 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. Démarrer avec réécriture

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. Réécrire se termine avec

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

Remarque:

La méthode ci-dessus: tous mis dans $ (function () {

La méthode ci-dessus

}) ;

Publié 26 articles originaux · loué 0 · visites 9934

Je suppose que tu aimes

Origine blog.csdn.net/weixin_38246518/article/details/79394417
conseillé
Classement