特别是我想定义本地 jQuery (var jQuery) where
应该存储 jQuery(以及本地 $)。
问题在于 jQuery 直接操作窗口对象:
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;
})(window);
这是对 jQuery 1.6.4 的引用
我该如何解决这个问题?
P.S.:我的特殊问题是为第 3 方网站使用编写一些片段
如果我包含 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);
关于javascript - 如何封装jQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7937507/