How to change Namespace of jQuery?

前端 未结 2 1574
执笔经年
执笔经年 2020-12-11 11:37

I\'m working on a library which uses jQuery internally which work fine. But I don\'t want force the user to use my jQuery version just because he uses my library in his code

相关标签:
2条回答
  • 2020-12-11 11:58

    You'd have to go into the source because window.jQuery is defined there, and just update the reference name.

    This seems like an unreasonable request though.

    Edit: Looks like all you have to do is set the reference to $.noConflict:

    <script src="jquery.js"></script>
    <script>
    var foo = {};
    foo.jQuery = jQuery.noConflict( true );
    alert( typeof jQuery );
    alert( typeof $ );
    alert( typeof foo.jQuery );
    </script>
    

    You'd have to do this right after you include the script though. My previous way did it directly in the script:

    http://173.45.226.115/jquery.html

    0 讨论(0)
  • 2020-12-11 12:16

    By specifying true in the jQuery.noConflict(true);, it sets the noConflict to extreme mode and should make it so it won't conflict with any other library. It should be the first thing you define anywhere I'm guessing, so no jQuery code/plugin should appear before the var mylib.jQuery = jQuery.noConflict(true);

    0 讨论(0)
提交回复
热议问题