如何封装jQuery? [英] How to encapsulate jQuery?

查看:72
本文介绍了如何封装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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆