how to use jquery.noConflict property

后端 未结 4 793
轻奢々
轻奢々 2020-12-09 00:39

I am testing my webpage on a server using preview dns. Just realized that preview dns automatically adds mootools library towards the end of any php page. Probably for thei

相关标签:
4条回答
  • 2020-12-09 00:58

    Insert the following before any jquery code:

    $j = jQuery.noConflict(true); 
    

    Now you can use $j instead of $ for any jquery stuff you want to do. i.e.:

    $j("body")
    
    0 讨论(0)
  • 2020-12-09 00:59

    I prefer using a self executing anonymous function to give your jQuery code its own scope where you can use $ as you normally would if you didn't have to worry about compatability.

    This is going to look weird if you haven't seen it before. Basically, what I did was define a function that takes a parameter (the $) and then execute that function with jQuery as the parameter.

    <script>
    jQuery.noConflict();
    
    (function($) {
        //use '$' as you normally would
        $(document).ready(function() {
            //code here that depends on the document being fully loaded
        });
    
    })(jQuery);  
    
    </script>
    
    0 讨论(0)
  • 2020-12-09 01:08

    Instead of j(function()...

    try

    j(document).ready
    (
      function()
      {
       alert("here");
       j("select#rooms")
         .change
         (
           function()
           {
             alert("here1");
           }
         )
      }
    );
    

    You were trying to wrap a function instead of an element, which is what might be causing the unexpected behavior.

    0 讨论(0)
  • 2020-12-09 01:12

    After you include jQuery, add the following in a script tag immediately after it.

    <script> 
    jQuery.noConflict();
    
    jQuery(document).ready(function() {
    alert('hi');
    });
    </script>
    

    Also place your jQuery script before your mootools library. Order will be:

    jQuery script include

    noConflict code

    mootools script include

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