繁体   English   中英

如何在 IE11 中支持 .closest

[英]How to support .closest in IE11

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

SCRIPT438:对象不支持“最接近”的属性或方法

即使我包括

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

在我的 rollup.js 中。

调用发生在此代码中:

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

为什么 polyfill 不存在? 但是,如何正确使用它? 我正在考虑用一个 IE11 支持替换 .closest。

使用 Polyfill

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM