![](/img/trans.png)
[英]How to solve in IE11: Object doesn't support property or method 'padStart'
[英]padStart() not working in IE11
我正在使用angularjs 1.7.2和kendo ui scheduler 。 所有路由在幾乎所有瀏覽器中都能正常工作,除非涉及到IE 11中的padStart()部分。當使用padStart代碼時,此錯誤會顯示出來
TypeError:Object不支持屬性或方法'padStart'
let ret = '#' + ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
有沒有辦法我們可以處理這個或另一種方法來實現padStart
IE 11不支持此功能。 請看一下: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart#Browser_compatibility
您正在尋找的是填充填充瀏覽器缺失功能的填充物。 以下代碼也來自developer.mozilla.org將幫助您:
// https://github.com/uxitten/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
也需要進行polyfilled,可以在這里找到: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/字符串/重復
或者包含以下代碼段:
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("");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.