Using JQuery and Prototype in the same page

前端 未结 12 1180
忘了有多久
忘了有多久 2020-11-30 03:39

Several of my pages use both JQuery and Protoype. Since I upgraded to version 1.3 of JQuery this appears to be causing problems, because both libraries define a function nam

相关标签:
12条回答
  • 2020-11-30 04:31

    Currently you can do something like this:

    <head>          
        <script type="text/javascript" src="/obp/js/prototype.js"></script>
        <script type="text/javascript" src="/obp/js/jquery.js"></script>
        <script type="text/javascript">
            var $j = jQuery.noConflict();
        </script>
    </head>
    

    Then, use jQuery as $j() and Prototype's $().

    0 讨论(0)
  • 2020-11-30 04:33

    You need to load it in your public/javascript/application.js

    jQuery.noConflict();
    
    var $j = jQuery;
    

    This is also a good article that may be helpful.

    JQuery & Prototype working together

    0 讨论(0)
  • 2020-11-30 04:35

    It would seem that the most simple answer would be to bite the bullet, and include your noConflict lines. Of course if your pages aren't using a shared header, that solution might not be the best.

    0 讨论(0)
  • 2020-11-30 04:39

    You could call jquery first and then set

    var $j = jQuery;
    

    prototype will take control of $ in this case.

    Or, you could just refer to jQuery by using the full jQuery function name (jQuery).

    0 讨论(0)
  • 2020-11-30 04:39

    Use Prototype below jQuery like this:

    <script type="text/javascript" src="news/jquery-1.2.3.pack.js"></script>
    <script type="text/javascript" src="news/jquery.easynews.js"></script>
    
    
    <script type="text/javascript" src="lb/js/prototype.js"></script>
    <script type="text/javascript" src="lb/js/scriptaculous.js?load=effects"></script>
    <script type="text/javascript" src="lb/js/lightbox.js"></script>
    <link href="lb/css/lightbox.css" rel="stylesheet" type="text/css" />
    

    in this case the jQuery function will create a problem, so you can use this to solve the problem:

    <script type="text/javascript">
         jQuery.noConflict();
         var JQ = jQuery;//rename $ function
    </script>
    
    0 讨论(0)
  • 2020-11-30 04:41

    This solution worked fine:

    jQuery.noConflict();
    var $j = jQuery;
    

    Now use $j in place of $ for your jQuery code, like:

    $j(document).ready(function() {
        $j('#div_id').innerfade({
             // some stuff
        });
    });
    
    0 讨论(0)
提交回复
热议问题