繁体   English   中英

Select2 Uncaught TypeError:无法读取未定义的属性“调用”

[英]Select2 Uncaught TypeError: Cannot read property 'call' of undefined

我正在尝试从 Select2 版本 4.0.7 升级到 4.0.13(或介于两者之间的任何版本)。 当我这样做时,单击 select 框时出现以下错误。

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;
            };
    }

暂无
暂无

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

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