JQuery conflict with an other JQuery library

前端 未结 5 979
南笙
南笙 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条回答
  •  渐次进展
    2021-01-02 05:16

    What you need to do to fix your problem is un-alias the jQuery function and assign it to another variable name (remember: variables can be functions). You need to use the jQuery.noConflict() function to un-alias the $() function. Here one one to do it:

    // ...after all of Joomla's JS is done executing...
    
    // before loading your version of jQuery var jquery = {}; // aka new Object()
    jquery.joomla = jQuery.noConflict(); // moves jQuery into another namespace
    
    // load your version
    

    Now, when you load your version, it will take over the jQuery and $ namespaces, but you'll still have the other reference to Joomla's jQuery function if you need it. To re-iterate, the basic flow is:

    1. Load Joomla's jQuery
    2. Run Joomla's jQuery-dependent code
    3. Move Joomla jQuery into another namespace
    4. Load your jQuery
    5. Execute your code using $()

提交回复
热议问题