如何在IE11中支持.closeest

问题描述 投票:0回答:1

我正在使用svelte+rollup+rollup-plugin-polyfill。

SCRIPT438: 对象不支持 "最接近 "属性或方法。

即使我加入了

polyfill(['@webcomponents/webcomponentsjs','element-closest']),

在我的rollup.js中。

调用发生在这段代码中。

function onDocumentClick (e) {
    if (!e.target.closest('.autocomplete')) close();
}

为什么polyfill不存在呢,如何正确使用它?我想用IE11支持的.closeest替换掉。

javascript internet-explorer svelte rollupjs
1个回答
0
投票

使用Polyfill([@webcomponentswebcomponentsjs],'...

if (!Element.prototype.matches) {
  Element.prototype.matches = Element.prototype.msMatchesSelector || 
                              Element.prototype.webkitMatchesSelector;
}

if (!Element.prototype.closest) {
  Element.prototype.closest = function(s) {
    var el = this;

    do {
      if (Element.prototype.matches.call(el, s)) return el;
      el = el.parentElement || el.parentNode;
    } while (el !== null && el.nodeType === 1);
    return null;
  };
}

最接近的细节和Polyfill

© www.soinside.com 2019 - 2024. All rights reserved.