javascript - 如何封装jQuery?

标签 javascript jquery

特别是我想定义本地 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/

相关文章:

php - 如何通过此文件上传传递 csrf token

javascript - JQuery 循环插件不工作

javascript - 使用 Javascript 使用相同的 id 选中复选框

带有 POST 数据的 Javascript XMLHttpRequest 上传文件

javascript - jQuery.InArray() 返回 -1 的问题

jquery - 是否可以通过单个 ajax 调用一次填充 2 个 jqGrid?

javascript - 从 jQuery Whatweather 获取描述

javascript - React.js 将对象键值对作为 props 传递

javascript - 在 iframe 中加载网页后设置 textarea val

javascript - 索引 Pouch db 多维文档