[英]How to use 2 versions of JQuery efficiently?
I have a web app that using JQuery 1.6, this can't be changed. 我有一个使用JQuery 1.6的Web应用程序,这是无法更改的。 I have a D3 viewer that needs a version atleast 1.8.
我有一个D3查看器,需要至少1.8版本。 I have found a solution as follows :
我找到了一个解决方案如下:
<script src='link to jquery 1.10' type="text/javascript"></script>
<script src='link to jquery custom 1.10' type="text/javascript"></script>
var jQuery_1_8_2= $.noConflict(true);
So where is use '$', i now use 'jQuery_1_8_2'. 那么在哪里使用'$',我现在使用'jQuery_1_8_2'。
This works great, but i have the plugin JStree. 这很好用,但我有插件JStree。
What i need is a function that basically when the viewer is run it changes every use of JQuery to the newer version. 我需要的是一个函数,基本上当查看器运行时,它会将JQuery的每次使用都更改为更新的版本。 So i dont have to go into the source code of the plugin and change every '$' character to 'jQuery_1_8_2'.
所以我不必进入插件的源代码并将每个'$'字符更改为'jQuery_1_8_2'。
You can order your JS files in a way that when JSTree loads there is only one version of JQuery available, it gets JQuery object only once, so there is no problem changing the value of global $
\\ jQuery
after it has loaded. 您可以按照以下方式订购JS文件:当JSTree加载时,只有一个版本的JQuery可用,它只获得一次JQuery对象,因此在加载后更改全局
$
\\ jQuery
的值没有问题。
<script src="jquery_for_jstree.js"></script>
<script src="jstree.js"></script>
<script>
var jsTreeJquery = $.noConflict(true);
</script>
<script src="another_jQuery.js"></script>
<script>
jsTreeJquery ("...").jstree(...)
</script>
You can see this approach working on http://jsfiddle.net/npd3kc5t/ 您可以在http://jsfiddle.net/npd3kc5t/上看到这种方法
This works because JSTree follows jQuery advice of protecting alias and adding scope . 这是有效的,因为JSTree遵循保护别名和添加范围的 jQuery建议。
In other words when the plugin loads, it gets the global jQuery
reference and creates a local variable $
only for it, so does not matter what do you do after plugin loaded with global $
\\ jQuery
. 换句话说,当插件加载时,它获取全局
jQuery
引用并仅为它创建一个局部变量$
,所以在使用全局$
\\ jQuery
加载插件后你做了什么并不重要。
Use jquery no conflict 使用jquery没有冲突
//new version
var newJquery = jQuery.noConflict();
//call it like this
newJquery('#example').on('click')......
For the old one do the samething 对于旧的人做同样的事情
var oldJquery = jQuery.noConflict();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.