我有一个需要支持IE11的Web应用程序。我正在使用ES6 +对其进行编码并使用babel-polyfill,因此它将与IE11兼容。我在IE11中可以正常使用箭头功能,匿名功能和其他ES6 +功能。但是,由于某些原因,element.classList.replace()方法不会被填充,或者是因为IE11似乎找不到它。
所以我想做的是获取html元素列表,循环每个元素,然后用另一个CSS类替换CSS类(很简单吧?)。嗯,IE11抛出以下错误“对象不支持属性或方法'替换'。想法?
Webpack版本:4.32.2, @ babel /核心:7.4.5, @ babel / polyfill:7.4.4
这是我的JS:
var invalidClassElement = document.querySelectorAll("[aria-owns*=".concat(id, "]"))[0].childNodes;
invalidClassElement.forEach(function (node) {
if (node.classList.contains('k-invalid')) {
node.classList.replace('k-invalid', 'k-default');
// The code below works in IE11.
// node.classList.remove('k-invalid');
// node.classList.add('k-default');
}
答案 0 :(得分:1)
babel-polyfill不对classList进行polyfill,并且IE11中对classList的支持受到限制。您还需要一个额外的polyfill,因为您提供的指向MDN的链接也表明了这一点。
也请参见以下现有答案:
Object doesn't support property or method 'replace' on Internet Explorer 11
还有一个相关的core-js问题(由babel-polyfill使用,因此说明了为什么未实现):https://github.com/zloirock/core-js/issues/287
答案 1 :(得分:1)
旧帖子,但是我做了一个可以在IE10-11上使用的polyfill。我需要一个,但是找不到一个:(
DOMTokenList.prototype.replace = function (a, b) {
var arr = Array(this);
var regex = new RegExp(arr.join("|").replace(/ /g, "|"), "i");
if (!regex.test(a)) {
return this;
}
this.remove(a);
this.add(b);
return this;
}