padStart() 在 IE11 中无法正常工作。

16
我正在使用AngularJS 1.7.2和Kendo UI Scheduler。在几乎所有浏览器中,所有路由都正常工作,除了IE 11中的padStart()部分。 当使用padStart代码时,会出现以下错误: TypeError:对象不支持属性或方法'padStart'
let ret = '#' + ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');

有没有一种方法可以处理这个问题或者替代实现{{padStart}}

1个回答

28

IE 11不支持此功能。请查看这里:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart#Browser_compatibility

你需要的是填充浏览器缺失函数的polyfills。 以下代码也来自developer.mozilla.org将会帮助你:

// https://github.com/behnammodi/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
    String.prototype.padStart = function padStart(targetLength,padString) {
        targetLength = targetLength>>0; //truncate if number or convert non-number to 0;
        padString = String((typeof padString !== 'undefined' ? padString : ' '));
        if (this.length > targetLength) {
            return String(this);
        }
        else {
            targetLength = targetLength-this.length;
            if (targetLength > padString.length) {
                padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
            }
            return padString.slice(0,targetLength) + String(this);
        }
    };
}

编辑:如评论中@Plaute所提到的,函数repeat还需要进行填充,可以在此处找到https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat

或者包含以下代码片段:

if (!String.prototype.repeat) {
  String.prototype.repeat = function(count) {
    'use strict';
    if (this == null) {
      throw new TypeError('can\'t convert ' + this + ' to object');
    }
    var str = '' + this;
    count = +count;
    if (count != count) {
      count = 0;
    }
    if (count < 0) {
      throw new RangeError('repeat count must be non-negative');
    }
    if (count == Infinity) {
      throw new RangeError('repeat count must be less than infinity');
    }
    count = Math.floor(count);
    if (str.length == 0 || count == 0) {
      return '';
    }
    // Ensuring count is a 31-bit integer allows us to heavily optimize the
    // main part. But anyway, most current (August 2014) browsers can't handle
    // strings 1 << 28 chars or longer, so:
    if (str.length * count >= 1 << 28) {
      throw new RangeError('repeat count must not overflow maximum string size');
    }
    var maxCount = str.length * count;
    count = Math.floor(Math.log(count) / Math.log(2));
    while (count) {
       str += str;
       count--;
    }
    str += str.substring(0, maxCount - str.length);
    return str;
  }
}

如果要避免依赖于String.prototype.repeat,可以使用以下代码:

padString += Array.apply(null, Array(targetLength)).map(function(){ return padString; }).join("");

2
函数 repeat 在 IE 11 中不存在,必须添加。请参考 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat。 - Plaute
这当然是真的。感谢您的补充 :) - r3dst0rm
10
IE 11 真是一团糟 :) - Plaute
如果您处于IE 11的F12控制台模式下,则存在一个问题。顶部的兼容性/版本框架可能会指示版本10。即使使用了polyfills,这也会失败。您需要将其更改为版本11。但是,请注意,如果用户将您的网站放入兼容性模式中,它仍然可能会出现故障。 - Newclique
@Wade,我刚把代码放进去了,在IE10上可以运行。可能是IE10的最新兼容性更新修复了这个问题,希望它们也在IE11上修复了。我不是特别在意,因为IE10很少被使用,但它是我最慢的IE测试机器。 - Robert Mauro
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接