选择2 未捕获的类型错误:无法读取未定义的属性'call'



我正试图从Select2版本4.0.7升级到4.0.13(或两者之间的任何版本(。当我点击选择框时,我会得到以下错误。

Uncaught TypeError: Cannot read property 'call' of undefined
at r.option (select2.min.js:formatted:489)
at r.append (select2.min.js:formatted:425)
at d.<anonymous> (select2.min.js:formatted:530)
at d.e.invoke (select2.min.js:formatted:289)
at d.e.trigger (select2.min.js:formatted:283)
at d.trigger (select2.min.js:formatted:3436)
at select2.min.js:formatted:3353
at n.query (select2.min.js:formatted:2138)
at d.<anonymous> (select2.min.js:formatted:3352)
at d.e.invoke (select2.min.js:formatted:289)

当我改回4.0.7版本时,一切都恢复到预期的工作状态。这种情况发生在站点上的所有select2框上,而不管它是用什么选项初始化的。我试过完整版和常规版。

我使用的是jQuery 3.5.1版

更新

经过进一步检查,项目中有一些代码覆盖了Window.Element。我修复了这些代码,不再需要下面的polyfill。

原始答案

我不知道为什么,但我发现我需要添加一个polyfill才能使其工作。我使用的是Chrome版本87.0.4280.141(官方版本((64位(,但由于某种原因,Element.prototype.matches返回未定义。我从MDN添加了以下Polyfill来解决问题:

//Select2 JS Polyfill
if (!Element.prototype.matches) {
Element.prototype.matches =
Element.prototype.matchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector ||
Element.prototype.oMatchesSelector ||
Element.prototype.webkitMatchesSelector ||
function (s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i = matches.length;
while (--i >= 0 && matches.item(i) !== this) { }
return i > -1;
};
}

最新更新