如何封装jQuery? [英] How to encapsulate jQuery?
问题描述
我特别想在其中定义本地jQuery(var jQuery)
jQuery应该被存储(以及本地$).
Particularly I want to define local jQuery (var jQuery) where
jQuery should be stored (and also local $).
问题是jQuery直接与窗口对象一起操作:
The problem is that jQuery operates directly with window object:
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;
})(window);
这是jQuery 1.6.4的引文
我该如何解决?
this is a citation from jQuery 1.6.4
How can I workaround this?
P.S .:我的特殊问题是为第三方网站使用编写一些代码段
如果我包含jQuery,可能会与
第三方js代码.目前,我正在执行以下操作:
P.S.: my particular problem is to write some snippet for 3-rd party website use
and if I include jQuery there may appear some incompatibilities with
that 3-rd party js code. Currently I'm doing the following:
// here some license notes about jQuery included
(function() {
if (typeof jQuery === "undefined") {
// jQuery code as it is goes here
}
// my code
})();
推荐答案
您可以将true
传递给 $ .noconflict() 让jQuery从全局范围内删除其所有变量:
You can pass true
to $.noconflict() to have jQuery remove all its variables from the global scope:
(function($) {
var jQuery = $.noconflict(true);
// From there on, window.jQuery and window.$ are undefined.
var $ = jQuery;
// Do something with the local jQuery and $...
})(jQuery);
这篇关于如何封装jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!