简体   繁体   中英

Forced to use jQuery instead of $

I'm was doing my page with jquery scripting all of then written like $. , $( My sample code like here

<script src="http://code.jquery.com/jquery-latest.js"></script>

function dome(){
  $.ajax({
     async: false,
     type: 'POST',
     url: 'test.php',
     success: function(data) {
     alert('ok');
     }
});
}

But I also have to use a Prototype library, then after add code <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>

My above function did not work any longer The error report from firebug is TypeError: $.ajax is not a function Then only way to solve this is to change $.ajax({ to jQuery.ajax({

Do you know how can I manage to do coding as short like I did before ($.)?

You have to define $ in your code to use it; jQuery.noConflict(); will also do the trick. Check the Internet on how to set $ as a jQuery operator.

<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>    
    (function($){ // remap '$' to jQuery

       $(function(){  // DOM ready
           // YOUR STUFF HERE
       });

    })(jQuery); 
</script>

Or take a look at jQuery.noconflict From the docs:

jQuery.noConflict();
// Do something with jQuery
jQuery("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';

use jQuery.noConflict(); in your code.

read here http://api.jquery.com/jQuery.noConflict/

Assign something else to Jquery like

var $_ = jQuery

then use $_.ajax()

jQuery.noConflict(); is the exact thing you're looking for to separate library uses. See here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM