JQuery conflict with an other JQuery library

前端 未结 5 998
南笙
南笙 2021-01-02 05:07

I use jquery for a module. My joomla template have an integrated jquery menu. So they conflict with each other.

Is there a way to solve this problem. Following the s

5条回答
  •  猫巷女王i
    2021-01-02 05:28

    Just to wrap your jq script in a self-invoking function.

    (function($){
       $(document).ready(function(){
    
          $('div').click( function(){ alert('ding'); });
    
       });
    })(jQuery);
    

    This creates a private scope so you don't have to worry about any collisions. You can skip all the uneasyness with the jQuery.noConflict(); solution and you don't have to give up that wonderful $! I do this as a matter of habbit when I know there are other chunks of code at work (eg, any cms) - even if a new extension is added after my testing, it shouldn't break my jQuery.

    There's a great overview in the jQuery Cookbook (http://listic.ru/jQuery_Cookbook.pdf - chapter 1.17). If it's good enough for Resig, I guess it will work for me!

提交回复
热议问题