简体   繁体   English

如何使用jQuery noconflict?

[英]How to use jQuery noconflict?

First thread: 第一个线程:

https://stackoverflow.com/questions/15526845/function-idreturn-document-getelementbyidid-blows-up-jquery https://stackoverflow.com/questions/15526845/function-idreturn-document-getelementbyidid-blows-up-jquery

There's a conflict with my 2 JavaScript files. 我的2个JavaScript文件有冲突。

I tried to use no.conflict but I don't understand which function I need to change... my scripts loads like this 我尝试使用no.conflict,但是我不明白我需要更改哪个功能...我的脚本加载如下

<script src="<?php echo Config::$absolutePath; ?>media/jquery-1.4.3.min.js"></script>
<script src="<?php echo Config::$absolutePath; ?>media/tags.js"></script>
<script src="<?php echo Config::$absolutePath; ?>js/zoom.js"></script> 
<script type="text/javascript">
$.noConflict();
// Code that uses other library's $ can follow here.
$(id){return document.getElementById(id)};
</script>
<script src="<?php echo Config::$absolutePath; ?>media/pixelicious.js"></script> 
<script type="text/javascript" src="<?php echo Config::$absolutePath; ?>media/dddropdownpanel.js"></script>

What's wrong, what I need to change? 怎么了,我需要改变什么?

You have a $('body').zoom({ }) at the end of the second file, this have to be changed to jQuery('body').zoom({ }) . 在第二个文件的末尾有一个$('body').zoom({ }) ,必须将其更改为jQuery('body').zoom({ })

Your self executing anonymous function does not cover this piece of code. 您的自执行匿名函数不覆盖这段代码。

Well you can use like this. 好吧,您可以像这样使用

jQuery.noConflict();

But if you are using something like Joomla there is some pluggins that will do all the work for you.. This works well: 但是,如果你正在使用的东西如Joomla有一些pluggins会做所有的工作对你.. 这非常适用:

When you use noConflict all $() that are meant to invoke jquery must become jQuery() 当您使用noConflict时,所有用于调用jquery的$()必须变为jQuery()

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

As others suggested: 正如其他人建议的那样:

  1. Change $.noConflict() to jQuery.noConflict() . $.noConflict()更改$.noConflict() jQuery.noConflict()
  2. In the second file change $('body).zoom({}) to jQuery('body').zoom({}) 在第二个文件中,将$('body).zoom({})更改为jQuery('body').zoom({})

Those two should solve all the problems with two files. 那两个应该用两个文件解决所有问题。 And remember that whenever you want to call jQuery function you have to do it with jQuery not $ . 请记住,每当您要调用jQuery函数时,都必须使用jQuery而不是$

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

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