特别是我想定义本地 jQuery (var jQuery) 在哪里
应该存储 jQuery(以及本地 $)。
问题是 jQuery 直接操作 window 对象:
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;
})(window);
这是来自 jQuery 1.6.4 的引用
我该如何解决这个问题?
P.S.:我的特殊问题是为 3-rd 方网站使用编写一些片段
如果我包含 jQuery,可能会出现一些与
那个第 3 方 js 代码。目前我正在做以下事情:
// here some license notes about jQuery included
(function() {
if (typeof jQuery === "undefined") {
// jQuery code as it is goes here
}
// my code
})();
最佳答案
您可以将 true
传递给 $.noconflict() 以让 jQuery 从全局范围中删除其所有变量:
(function($) {
var jQuery = $.noconflict(true);
// From there on, window.jQuery and window.$ are undefined.
var $ = jQuery;
// Do something with the local jQuery and $...
})(jQuery);