我有一个使用 jQuery 1.8 编写的 javascript 小部件,该小部件加载到使用 jQuery 1.3 的客户端网站上。如果没有一些非常丑陋的黑客攻击,我就无法让 noConflict 在 IE 中正常工作。这是我的小部件呈现后页面的样子:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.3.min.js"></script>
</head>
<html>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script>
$jq = $.noConflict();
</script>
</body>
</html>
在 Chrome 中,这很好用,$jq 被定义为 jQuery 1.8.3,但在 IE 8 中这不起作用,$jq 被定义为 jQuery 1.3。我的猜测是,这是一个计时问题,脚本在新的 jQuery 加载之前执行。我放入了一些回调计时器并且它可以工作,但我无法想象嵌入回调计时器是正确的方法。
有什么想法吗?
编辑
我无法命名 jQuery1.3,客户端在整个页面中使用它并且不想升级。
最佳答案
<!-- load jQuery 1.5 -->
<script type="text/javascript" src="http://foo.com/jquery-1.5.js"></script>
<script type="text/javascript">
var jQuery_1_5 = $.noConflict(true);
</script>
<!-- load jQuery 1.6 -->
<script type="text/javascript" src="http://foo.com/jquery-1.6.js"></script>
<script type="text/javascript">
var jQuery_1_6 = $.noConflict(true);
</script>
在这种情况下 -
使用
jQuery_1_6(document).ready(function($) {
// Code using $ as usual goes here.
});
而不是
jQuery(document).ready(function($) {
// Code using $ as usual goes here.
});
请参阅此处 - http://blog.nemikor.com/2009/10/03/using-multiple-versions-of-jquery/
关于javascript - jQuery 多版本在 IE 中无冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14190921/