How Can I remove Conflict in my Jquery?

2019-02-20 19:11发布

问题:

I am new to JQuery And I have used 2 jQueries in my page.

For first JQuery my script is like this:

<script>
    var jq = $.noConflict();
    jq(function() {
         jq( "#my_tabs" ).tabs({
         event: "click" //click
         });            
    });
</script>

Now when I use a second jQuery like this I lost my first jQuery. It's unable to load

 <script type="text/javascript">
  $(document).ready(function () {
    $("#waterwheel-carousel-default").waterwheelCarousel();

    $("#waterwheel-carousel-higharch").waterwheelCarousel({
        startingWaveSeparation: -90,
        waveSeparationFactor: .7,
        centerOffset: 10,
        startingItemSeparation: 120,
        itemSeparationFactor: .9,
        itemDecreaseFactor: .75
    });

    $("#waterwheel-carousel-horizon").waterwheelCarousel({
        startingWaveSeparation: 0,
        centerOffset: 30,
        startingItemSeparation: 150,
        itemSeparationFactor: .7,
        itemDecreaseFactor: .75,
        opacityDecreaseFactor: 1,
        autoPlay: 1500
    });

    $("#waterwheel-carousel-flat").waterwheelCarousel({
        itemSeparationFactor: 1,
        itemDecreaseFactor: 1,
        waveSeparationFactor: 1,
        startingWaveSeparation: 0,
        startingItemSeparation: 280,
        centerOffset: 10,
        opacityDecreaseFactor: .3,
        autoPlay: 3000,
        edgeReaction: 'reverse'
    });

    $("#waterwheel-carousel-vertical").waterwheelCarousel({
        orientation: 'vertical',
        startingItemSeparation: 100,
        startingWaveSeparation: 40,
        autoPlay: 2000
    });

  });
</script>

I am using both jQueries for image sliding.. Can anyone tell me what the problem is? Thanks

回答1:

It is your 2nd jQuery script you should be performing $.noConflict() on.

If you do;

<script src="http://code.jquery.com/jquery-1.7.js"></script>
<script>
    var jq = $.noConflict();
    jq(function() {
         jq( "#my_tabs" ).tabs({
         event: "click" //click
         });            
    });
</script>
<script src="http://code.jquery.com/jquery-1.4.js"></script>

Both jQuery and $ will point to jQuery 1.4, and nothing will refer to jQuery 1.7. Incidentally, if you were to run $.noConflict again after loading jQuery 1.4, jQuery would reference 1.4, but $ would be undefined.

However, if you do:

<script src="http://code.jquery.com/jquery-1.7.js"></script>
<script src="http://code.jquery.com/jquery-1.4.js"></script>
<script>
    var jq = $.noConflict();
    jq(function() {
         jq( "#my_tabs" ).tabs({
         event: "click" //click
         });            
    });
</script>

The $ will refer to jQuery 1.7, but the jQuery will point to jQuery 1.4 (as will your jq variable).

You may want to look at the $.noConflict(true); which releases both the jQuery and $ variables; so you can do something like this;

<script src="http://code.jquery.com/jquery-1.7.js"></script>
<script src="http://code.jquery.com/jquery-1.4.js"></script>
<script>
    var jq = $.noConflict(true);
    jq(function() {
         jq( "#my_tabs" ).tabs({
         event: "click" //click
         });            
    });
</script>

Then both the $ and jQuery will point to jQuery 1.7, and only your jq variable will point to jQuery 1.4