jquery - my multiple versions of jQuery doesn't work
问题描述
I would like to use multiple versions of jQuery on the same page. I wrote below but two scripts doesn't work. Could you teach me what is worong my code pleaes?
<script type="text/javascript" src="//jpostal-1006.appspot.com/jquery.jpostal.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<script type="text/javascript">
var $330 = $.noConflict(true);
</script>
Here is my whole JS part
https://jsfiddle.net/blueink/8m7oLzsf/1/
one is postal code script which uses jquery-2.1.0.min.js and jpostal.js second script is livesearch which uses 3.3.0.
解决方案
The problem is because you need to call $.noConflict()
on the first instance of jQuery before you add the second. Therefore if you want to keep $
as a reference to 2.1.0, add that second.
Also note that your reference to jquery.jpostal.js
, which relies on jQuery, needs to be placed after both of these references.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<script type="text/javascript">
var $330 = $.noConflict(true);
</script>
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="//jpostal-1006.appspot.com/jquery.jpostal.js"></script>
<!-- Just for demonstration purposes -->
<script>
console.log($.fn.jquery)
console.log($330.fn.jquery);
</script>
With all of that said, requiring multiple versions of jQuery in one site is far from ideal, and will become a maintenance problem in the future. Your time would be well spent addressing the issues you have upgrading to the latest version.
推荐阅读
- css - 尝试在 PlayWright API 中搜索网站上的文本
- postgresql-13 - pghoard1.2 服务未启动
- php - Laravel 服务提供者没有加载 Collection 方法 Undefined method 'values'.intelephense(1013)
- design-patterns - 每个应用程序的模块还是每个计算、网络或存储的模块?- 基础设施即代码
- spring - Spring Boot Rest API + JPA
- sql - 多列占总数的百分比
- mysql - 指示mysql以读取权限输出CSV?(osx)
- android-11 - 如何在 Android 11 中访问(r+w)/sdcard/Android/data 文件夹
- python - 创建 Python 函数以迭代 List/DataFrame (VIF)
- java - 无法在 localhost 中运行 google app engine java 项目如何修复 java.util.logging.config.file 错误