脚本引用导致冲突 [英] script reference causes conflict

查看:100
本文介绍了脚本引用导致冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码1在头部执行:

I have code 1 which executes with this in the head:

<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>

然后我输入代码2:

<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script src="livesearch.js" type="text/javascript"></script>

问题是当我把最后一个引用(代码2)放在第一个引用时不再有效..
我做错了什么?

The problem is that when I put the last reference (code 2) the first doesn't work anymore.. What am I doing wrong?

(由于某种原因,代码2对jquery 1.7没有反应)

( for some reason code 2 doesnt react on jquery 1.7 )

推荐答案

您需要的是: jQuery.noConflict


如果有必要,我们可以通过传递真实 $ b释放jQuery名称$ b作为方法的参数。这很少是必要的,如果我们必须
这样做(例如,如果我们需要在同一页面上使用
jQuery库的多个版本)

If necessary, we can free up the jQuery name as well by passing true as an argument to the method. This is rarely necessary, and if we must do this (for example, if we need to use multiple versions of the jQuery library on the same page)

例如:

<script class="jsbin" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="javascript">
// now you alias the v1.7 jQuery to jQuery17
jQuery17 = jQuery.noConfict(true);
</script>

<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="javascript">
// now you alias the v1.4 jQuery to jQuery14
jQuery14 = jQuery.noConfict(true);
</script>

并将两部分代码放在不同的范围内。

And put you two parts of code in different scopes.

(function($){
//put your codes here which need jQuery 1.7 version
.....
})(jQuery17);

(function($){
//put your codes here which need jQuery 1.4 version
.....
})(jQuery14);

这篇关于脚本引用导致冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆