如何解决多个版本的 jquery 之间的冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15126912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:25:07  来源:igfitidea点击:

How to resolve conflict between multiple versions of jquery?

jquery

提问by Apelavin

I realize there are already various answers out there to questions about using multiple jquery versions on a page, but I have not been able to figure out how to apply them.

我意识到已经有关于在页面上使用多个 jquery 版本的问题的各种答案,但我一直无法弄清楚如何应用它们。

On my page I have two .js files - one for operating a collapsible menu, and the second for a thickbox gallery. The collapsible menu stopped working when I added the gallery. When I tried to use the no conflict solution from the link below, the menu worked but the gallery images would no longer load.

在我的页面上,我有两个 .js 文件 - 一个用于操作可折叠菜单,第二个用于厚盒画廊。当我添加画廊时,可折叠菜单停止工作。当我尝试使用下面链接中的无冲突解决方案时,菜单工作,但图库图像将不再加载。

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

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

Any ideas of what I'm doing wrong? Do I need to add things to both the .html file and the .js files?

关于我做错了什么的任何想法?我需要向 .html 文件和 .js 文件添加内容吗?

code:

代码:

<script type="text/javascript" src="../../01 Main/includes/jquery-1.6.min.js"></script>
<script type="text/javascript" src="../../01 Main/includes/main.js"></script>
<script type="text/javascript">
<div id="log">
  <h3>Before $.noConflict(true)</h3>
</div>
</script>

<script type="text/javascript" src="../scripts/jquery-latest.js"></script>
<script type="text/javascript" src="../scripts/thickbox.js"></script>
<script type="text/javascript">
var $log = $( "#log" );

$log.append( "2nd loaded jQuery version ($): " + $.fn.jquery + "<br>" );

jq16min = jQuery.noConflict(true);

$log.append( "<h3>After $.noConflict(true)</h3>" );
$log.append( "1st loaded jQuery version ($): " + $.fn.jquery + "<br>" );
$log.append( "2nd loaded jQuery version (jqlatest): " + jqlatest.fn.jquery + "<br>" );
</script>

also tried:

也试过:

<script type="text/javascript" src="../../01 Main/includes/jquery-1.6.min.js"></script>
<script type="text/javascript" src="../../01 Main/includes/main.js"></script>
<script type="text/javascript">
var jQuery_1_6_min = $.noConflict(true);
</script>

<script type="text/javascript" src="../scripts/jquery-latest.js"></script>
<script type="text/javascript" src="../scripts/thickbox.js"></script>
<script type="text/javascript">
var jQuery_latest = $.noConflict(true);
</script>

回答by Brad

it doesn't seem smart at all to use multiple versions of jquery but if you must, you could assign one of the versions to another global variable ie link to the first versions

使用多个版本的 jquery 似乎一点也不聪明,但如果必须,您可以将其中一个版本分配给另一个全局变量,即链接到第一个版本

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
window._$ = jQuery; 

then load the next version;

然后加载下一个版本;

(function(){
  var newscript = document.createElement('script');
     newscript.type = 'text/javascript';
     newscript.async = true;
     newscript.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js';
  (document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(newscript);
})();

回答by Mortalus

it is a possible duplicate to : Can I use multiple versions of jQuery on the same page?

它可能重复:我可以在同一页面上使用多个版本的 jQuery 吗?

ins short his answer:

简而言之,他的回答是:

<!-- load jQuery 1.1.3 -->
<script type="text/javascript" src="http://example.com/jquery-1.1.3.js"></script>
<script type="text/javascript">
var jQuery_1_1_3 = $.noConflict(true);
</script>

<!-- load jQuery 1.3.2 -->
<script type="text/javascript" src="http://example.com/jquery-1.3.2.js"></script>
<script type="text/javascript">
var jQuery_1_3_2 = $.noConflict(true);
</script>

Had to go through the same thing :)

不得不经历同样的事情:)