jQuery——多库共存

Posted 站错队了同志

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jQuery——多库共存相关的知识,希望对你有一定的参考价值。

多库共存:jQuery占用了$ 和jQuery这两个变量。当在同一个页面中引用了jQuery这个js库,并且引用的其他库(或者其他版本的jQuery库)中也用到了$或者jQuery这两个变量,那么,要保证每个库都能正常使用,这时候就有了多库共存的问题。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-1.8.2.min.js"></script>
    <script src="jquery-1.11.1.js"></script>
    <script>
        jQuery(function () {
            //打印版本号。
            console.log($.fn.jquery);//1.11.1
            $.noConflict();//让1.11.1放弃$的使用权,这样1.8.2就会获得对$的使用权
            console.log($.fn.jquery);//1.8.2
            console.log(jQuery.fn.jquery);//1.11.1
        })
    </script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-1.8.2.min.js"></script>
    <script src="jquery-1.11.1.js"></script>
    <script>
        jQuery(function () {
            console.log($.fn.jquery);//1.11.1
            var MrLv = $.noConflict(true);//让1.11.1放弃两个符号的使用权,同时定义一个新的使用权
            console.log($.fn.jquery);//1.8.2
            console.log(jQuery.fn.jquery);//1.8.2
            console.log(MrLv.fn.jquery);//1.11.1
        })
    </script>
</head>
<body>
</body>
</html>

 

以上是关于jQuery——多库共存的主要内容,如果未能解决你的问题,请参考以下文章

jQuery多库共存问题解决方法

jQuery内置动画和多库共存

jQuery插件扩展与多库共存

多库共存-冲突问题

jQuery-核心函数

jQuery 事件其他方法