更改第三方组件的 jQuery 命名空间

Change jQuery namespace of a 3rd party component

本文关键字:jQuery 命名空间 组件 第三方      更新时间:2023-09-26

当我们使用较旧的jQuery版本(1.4(和较新的版本(1.11(时,我们有2个不同的命名空间。 1.4 使用标准 $,1.11 使用 jQuery1111。

现在我正在尝试实现 froala 编辑器,它需要使用 1.11 版本。我已将 froala 代码更改为此代码以实现这一目标:

(function (a) {
    "function" == typeof define && define.amd ? define(["jquery"], a) : "object" == typeof module && module.exports ? module.exports = function (b, c) {
        return void 0 === c && (c = "undefined" != typeof window ? require("jquery") : require("jquery")(b)), a(c), c
    } : a(jQuery)
}(function (a) {
...
}(window.jQuery1111)));

但这给了我错误a is not a function(但脚本似乎可以运行(。错误在上面脚本的第 4 行捕获。

如果我将第 4 行的a(jQuery)更改为jQuery1111它运行没有错误,但我不确定这是否正确,或者以后是否会导致错误。

这是在非默认的jQuery命名空间中实现第三方组件的正确方法吗?

更新:脚本顺序

内部<head />

<script src="/js/jquery.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var jQuery1111 = jQuery.noConflict(true);
    window.jQuery1111 = window.jQuery1111 || jQuery1111;
</script>

<body />

<script type="text/javascript" src="/scripts/froala_editor.min.js"></script>

您可以更改脚本包含的顺序以解决此问题,而无需修改第三方库

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/froala-editor/2.1.0/js/froala_editor.min.js"></script>
<script type="text/javascript">
  var jQuery1111 = jQuery.noConflict(true);
  window.jQuery1111 = window.jQuery1111 || jQuery1111;
</script>