【问题标题】:select2 Uncaught TypeError: Cannot read property 'fn' of undefinedselect2 Uncaught TypeError:无法读取未定义的属性“fn”
【发布时间】:2015-03-22 21:39:35
【问题描述】:

我在 select2-script 上遇到了这个错误。

完整的堆栈跟踪显示:

Uncaught TypeError: Cannot read property 'fn' of undefined
select2.full.js?ver=4.1:5830 
(anonymous function) select2.full.js?ver=4.1:17 
S2 select2.full.js?ver=4.1:19 
(anonymous function) select2.full.js?ver=4.1:19 (anonymous function)

我认为这一定与 jQuery 在非冲突模式下加载有关(它是一个 Wordpress 网站),但我不知道出了什么问题。

第 17 - 19 行是:

factory(jQuery);
  }
}(function (jQuery) {

怎么了?使用 select2 4.0 RC2。

我必须补充一点,一切都按预期工作 - 但控制台上的堆栈跟踪并不好......

【问题讨论】:

  • 这是a known issue,已在最新主版本中修复,在即将发布的最终版本中不存在。

标签: jquery jquery-select2


【解决方案1】:

作者几天前修复了这个错误(Fix TypeError)。在 Github 上查看这个提交:https://github.com/select2/select2/commit/1f62eb67e89b5b72e9aa91586c5c22d76ffc7ed9

顺便说一下,正确的代码应该是:

jQuery.fn.select2.amd = S2;

【讨论】:

    【解决方案2】:

    好的,即使还没有答案,有时在这里发帖也会有所帮助:)

    将第 5830 行 $.fn.select2.amd = S2; 包装在一个无冲突安全的包装器中,如下所示:

    jQuery(function($) {
        $.fn.select2.amd = S2;
    });
    

    并且错误消失了。我认为必须/将在 4.0 最终版本中进行更改?!

    【讨论】:

      【解决方案3】:

      我有同样的错误并在 wordpress 中修复了它。 Jquery 库在 bootstrap-modal.js 库之前加载。 如果您通过以下方法添加脚本:

      add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style', 5 );
      wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
      wp_enqueue_script('popup' , get_template_directory_uri() . '/js/bootstrap-modal.js', array(), null );
      

      如果您使用相同的方法,请将您的加载优先级从 5 更改为例如 90。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-10
        • 2021-04-28
        • 2017-07-20
        • 1970-01-01
        • 2020-03-28
        • 2023-04-11
        • 2016-04-22
        • 2015-06-08
        相关资源
        最近更新 更多