简体   繁体   English

强制使用jQuery而不是$

[英]Forced to use jQuery instead of $

I'm was doing my page with jquery scripting all of then written like $. 我正在用jquery脚本编写页面,然后全部写成$. , $( 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> 但是我还必须使用原型库,然后添加代码<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({ 我上面的函数不再起作用了firebug的错误报告是TypeError:$ .ajax不是一个函数,然后解决此问题的唯一方法是将$.ajax({更改为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. 在Internet上查看如何将$设置为jQuery运算符。

<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

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

use jQuery.noConflict(); 使用jQuery.noConflict(); in your code. 在你的代码中。

read here http://api.jquery.com/jQuery.noConflict/ 在这里阅读http://api.jquery.com/jQuery.noConflict/

Assign something else to Jquery like 为Jquery分配其他内容,例如

var $_ = jQuery

then use $_.ajax() 然后使用$_.ajax()

jQuery.noConflict(); is the exact thing you're looking for to separate library uses. 您正在寻找分开图书馆用途的确切东西。 See here 看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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