有没有办法将Bootstrap 3.0插件与jQuery.noConflict()一起使用?

有没有办法将Bootstrap 3.0插件与jQuery.noConflict()一起使用?

问题描述:

我们当前正在页面上加载2个不同版本的jQuery,即1.4.2和1.10.1. $和window.jQuery对象当前指向1.4.2.

We are currently loading 2 different versions of jQuery on our page, 1.4.2 and 1.10.1. The $ and window.jQuery objects are currently pointing to 1.4.2.

我们在1.10.1版中使用noConflict()将其设置为$ jq1:

We are using noConflict() with version 1.10.1 to set it to $jq1:

var $jq1 = jQuery.noConflict(true);

有什么办法让Bootstrap 3.0插件自动使用$ jq1而不是$或window.jQuery?

Is there any way to get Bootstrap 3.0 plugins to automatically use $jq1 instead of $ or window.jQuery?

如果在加载jQuery 1.10.1版后直接加载引导JS,然后将jQuery置于无冲突模式,则它应该可以工作.

If you load the bootstrap JS straight after loading jQuery version 1.10.1 and then put jQuery into no conflict mode, it should work.

例如:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- Load any Bootsrap JS files before calling jQuery.noConflict()  -->
<script src="bootstrap.js"></script>
<script>
// Put jQuery 1.10.2 into noConflict mode.
var $jq1 = jQuery.noConflict(true);
</script>

<!-- This can be before or after the above -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

jQuery.noConflict(true)会将$jQuery都重新分配为其以前的值,因此无论是否首先加载1.4.2版本都没有关系.

jQuery.noConflict(true) will reassign both $ and jQuery to their previous values so it doesn't matter if version 1.4.2 is loaded first or not.

这确实意味着您的用户将下载jQuery两次,并且您需要记住在使用jQuery进行任何操作时是使用$jq1还是$.

It does mean your users will be downloading jQuery twice though and you will need to remember if to use $jq1 or $ when doing anything with jQuery.