简体   繁体   English

当我有 2 个 jquery 库时,文档就绪无法工作

[英]document ready not working when I have 2 jquery libraries

I used a plugin that includes我使用了一个插件,其中包括

jquery-1.12.4.min.js jquery-1.12.4.min.js

as one of its resources.作为其资源之一。 But in my project, I am using但在我的项目中,我使用

jquery-3.4.1.min.js. jquery-3.4.1.min.js。

In order for them to not have a conflict, I did this:为了让他们没有冲突,我这样做了:

<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script>var $x = jQuery.noConflict();alert("Version: "+$x.fn.jquery);</script> 
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>var $y = jQuery.noConflict();alert("Version: "+$y.fn.jquery);</script>

Even if the function noConflict() is working - due to the fact that the version of the respective jqueries can be seen on alert, I get an error pointing to the $(document).ready(function(){ . I guess I have to specify which version will I use? If yes, how? I can't find for the syntax to do it. Please help...即使函数noConflict()正在工作 - 由于可以在警报中看到相应 jqueries 的版本这一事实,我也会收到指向$(document).ready(function(){ 。我想我有指定我将使用哪个版本?如果是,如何?我找不到语法来做到这一点。请帮助...

First of all, better replace $.noConflict() with $.noConflict(true) to indicate that you want all jQuery instances to be removed from the conflicted name.首先,最好将$.noConflict()替换$.noConflict() $.noConflict(true)以表明您希望从冲突名称中删除所有 jQuery 实例。

All of that, makes it possible to use $y and $x to refer jQuery with an exact version.所有这一切,使得使用$y$x来引用具有确切版本的 jQuery 成为可能。

So instead of $(document).ready(function(){所以而不是$(document).ready(function(){

You would do $y(document).ready(function(){ or $x(document).ready(function(){你会做$y(document).ready(function(){$x(document).ready(function(){

You have used the noConflict function properly but you cannot use '$' in variable name, as calling noConflict function released '$' variable to be used by another javascript frameworks.您已正确使用 noConflict 函数,但不能在变量名中使用 '$',因为调用 noConflict 函数会释放 '$' 变量以供其他 javascript 框架使用。

change your variable names to x an y and give it a try.将您的变量名称更改为 x 和 y 并尝试一下。

Also you cannot use $(document).ready(function(){ as you have already changed the alias of the jquery $ variable to x and y in your case.您也不能使用$(document).ready(function(){因为在您的情况下,您已经将 jquery $ 变量的别名更改为 x 和 y 。

 <html> <head> <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <script>var x = jQuery.noConflict();</script> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script>var y = jQuery.noConflict();</script> <script type="text/javascript"> x(document).ready(function(){ alert('ready from :' + x.fn.jquery); }); y(document).ready(function(){ alert('ready from :' + y.fn.jquery); }); </script> </head> </html>

Thank you for your efforts.感谢你付出的努力。 I have figured out how to retain the $ on the former codes that I wrote.我已经想出了如何在我写的以前的代码上保留 $ 。 What I did was:我所做的是:

<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script>var $ = jQuery.noConflict();alert("Version: "+$x.fn.jquery);</script> 
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>var $y = jQuery.noConflict();alert("Version: "+$y.fn.jquery);</script>

Since I used $ as a container for the jquery version that I have been using for my project, I won't need to change the codes beginning with $ on my original codes.由于我使用 $ 作为我一直用于我的项目的 jquery 版本的容器,因此我不需要更改原始代码中以 $ 开头的代码。

But in order to use the older jquery version which was used by the plugin I integrated with my project, I must use $y ( ei $y(document).ready(function(){ }); and $y('#field1id').val(); ).但是为了使用我与我的项目集成的插件使用的旧 jquery 版本,我必须使用 $y ( ei $y(document).ready(function(){ }); 和 $y('#field1id ').val();)。

My project is already working now.我的项目现在已经开始工作了。 Thank you for the inputs.感谢您的投入。

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

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