Javascript 如何解决两个jquery冲突?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10978770/
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
How to resolve two jquery conflict?
提问by Vivek Parikh
I have a page which includes jquery for curtain effects and other for slider.the problem i am facing is my both jqueries are not working together.what should i do?
我有一个页面,其中包含用于窗帘效果的 jquery 和其他用于滑块的页面。我面临的问题是我的两个 jquery 无法协同工作。我该怎么办?
my sample code is
我的示例代码是
<!-- for slider -->
<script src="images/jquery-latest.js" type="text/javascript"></script>
<script src="images/jquery.bxslider2.min.js" type="text/javascript"></script>
<script src="images/scripts.js" type="text/javascript"></script>
<!-- for curtain -->
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script src="js/jquery.easing.1.3.js" type="text/javascript"></script>
回答by ThiefMaster
First of all, try using the current jQuery version (1.7.2)and see if both scripts work.
首先,尝试使用当前的 jQuery 版本 (1.7.2)并查看两个脚本是否都有效。
If not, consider using something that is compatible with the current jQuery version - if something requires jquery 1.3 it wasn't updated for a long time.
如果没有,请考虑使用与当前 jQuery 版本兼容的东西 - 如果某些东西需要 jquery 1.3,它很长时间没有更新。
If you really need both scripts and both requires different jQuery versions, then you'll need to do it like this:
如果你真的需要这两个脚本并且都需要不同的 jQuery 版本,那么你需要这样做:
<script src=".../jquery-1.3.2.js"></script>
<script src="script-that-needs-132.js"></script>
<script>var jQuery132 = $.noConflict(true);</script>
<script src=".../jquery-1.7.2.js"></script>
To use the old jQuery, then wrap the code, using it, like this:
要使用旧的 jQuery,请包装代码,使用它,如下所示:
(function($){
// here $ points to the old jQuery
})(jQuery132);
回答by slash197
Remove one of them, preferably this one <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
删除其中之一,最好是这个 <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
回答by Sunny
You can use
您可以使用
<script type="text/javascript" language="javascript">
var jq = jQuery.noConflict();
</script>
回答by acme
You can't/shouldn't include two different versions of jQuery. Stick to one (the most recent one should be the best choice). If your desired plugin does only support a very ancient version like 1.3 is, then you should choose a more recent one or start fiddling around in the code.
您不能/不应该包含两个不同版本的 jQuery。坚持一个(最近的一个应该是最好的选择)。如果你想要的插件只支持像 1.3 这样的非常古老的版本,那么你应该选择一个更新的版本或者开始在代码中摆弄。