問題描述
所以這個問題并不像一開始聽起來那么瘋狂.我正在開發(fā)一段 javascript 代碼以放入我的客戶頁面.我擔(dān)心的是他們是否在他們的網(wǎng)站上使用了另一個版本的 jQuery.
So this question isn't as crazy as it might sound at first. I'm developing a piece of javascript code to drop into my client's pages. What I'm worried about is if they are using another version of jQuery on their site.
我從 Jquery docs 知道這樣的東西應(yīng)該可以工作.
I know from the Jquery docs that something like this should work.
var dom = {};
dom.query = jQuery.noConflict(true);
問題是我也在使用一些 jquery 插件,即 fancybox 和 鼠標(biāo)滾輪.那么如何確保我的 jQuery 版本是唯一可以使用這些插件的版本.如果我們的一位客戶碰巧在他們的頁面中使用相同的但不同的版本,我不希望發(fā)生沖突.
The problem is I am also using some jquery plugins, namely fancybox and mousewheel. So how can I make sure my version of jQuery is the only one that can use those plugins. I don't want a conflict to happen if one of our clients happens to use the same ones in their pages but different versions.
我現(xiàn)在能想到的唯一確定的方法是修改插件,使它們只調(diào)用 dom.query
The only definite way I can think of right now is to modify the plugins so they only call dom.query
推薦答案
你可以使用一個技巧.如果您的 jQuery 版本是第二個加載的版本,您需要在加載它之前先移動到安全的版本,就像這樣
There is a trick you could use. If your version of jQuery is second which loads, you will need first move to safety theirs version before you load it, something like this
<script type="text/javascript">
(function($){
var theirs_jquery = $;
})(jQuery);
</script>
然后添加你的 jQuery 版本腳本包含標(biāo)簽
Then add your jQuery version script include tag
<script src="link/to/my/jquery.js"></script>
<!-- add your plugins -->
<script src="link/to/my/jquery/plugins/plugin1.js"></script>
<script src="link/to/my/jquery/plugins/plugin2.js"></script>
然后添加另一個腳本塊,如下所示
then add another script block with folowing
<script type="text/javascript">
(function(){
var $$ = jQuery; // this is your shortcut
var $ = theirs_jquery; // put back their version
})(jQuery);
</script>
在此之后,您可以使用 $$ 快捷方式訪問您的 jquery 版本
after this you can access your jquery version with $$ shortcut
這篇關(guān)于如果您在頁面中包含 2 個版本的 jQuery,您如何將插件限制為其中一個?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!