注意:我知道已经有人问过类似的问题here and here,但我正在寻找有关如何进行这项工作的更多说明或完全避免它的充分理由。
我正在向已使用旧版本 jQuery 库 (1.1.3.1) 的现有网站添加功能。我一直在针对最新版本的 jQuery 库 (1.4.2) 编写我的附加功能。我仅使用较新版本的 jQuery 测试了该网站,但它破坏了功能,因此现在我考虑在同一页面上使用这两个版本。这怎么可能?
我需要在代码中做什么来指定我使用的是一个版本的 jQuery 而不是另一个版本?例如,我将把<script>
在我的页面标题中添加了两个版本的 jQuery 标签,但是我需要做什么才能在我的调用代码中确定我正在调用库的一个版本或另一个版本?
也许是这样的:
//Put some code here to specify a variable that will be using the newer
//version of jquery:
var $NEW = jQuery.theNewestVersion();
//Now when I use $NEW, I'll know it's the newest version and won't
//conflict with the older version.
$NEW('#personName').text('Ben');
//And when I use the original $ in code, or simply 'jquery', I'll know
//it's the older version.
$('#personName').doSomethingWithTheOlderVersion();
更新:在阅读了一些答案后,我开始怀疑尝试玩这个游戏是否是一个好主意......
您可以使用 jQuery无冲突方法来实现这一点。
<script type="text/javascript" src="jquery-1.1.3.js"></script>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
//Here, $ refers to the most recent jquery script loaded, which is version 1.4.2.
$.noConflict();
//Here, jQuery refers to 1.4.2 and $ refers to 1.1.3
//You can setup an alias like so:
var $NEW = jQuery;
//Now, you can use old jQuery with $ and new jQuery with $NEW.
</script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)