twitter-bootstrap 有没有办法在 jQuery.noConflict() 中使用 bootstrap 3.0 插件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19573074/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 18:58:30  来源:igfitidea点击:

Is there a way to use bootstrap 3.0 plugins with jQuery.noConflict()?

javascriptjquerytwitter-bootstraptwitter-bootstrap-3

提问by protothe

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.

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

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

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

var $jq1 = jQuery.noConflict(true);

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

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

回答by Sam

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.

如果您在加载 jQuery 1.10.1 版本后直接加载 bootstrap JS,然后将 jQuery 置于无冲突模式,它应该可以工作。

e.g.:

例如:

<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)will reassign both $and jQueryto their previous values so it doesn't matter if version 1.4.2 is loaded first or not.

jQuery.noConflict(true)会将$和重新分配jQuery给它们以前的值,因此是否首先加载版本 1.4.2 并不重要。

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

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

回答by Jaime Montoya

I liked the explanation that the user "ajpiano" provided at https://forum.jquery.com/topic/multiple-versions-of-jquery-on-the-same-page:

我喜欢用户“ajpiano”在https://forum.jquery.com/topic/multiple-versions-of-jquery-on-the-same-page提供的解释:

<script src='jquery-1.3.2.js'></script>
<script>
    var jq132 = jQuery.noConflict();
</script>
<script src='jquery-1.4.2.js'></script>
<script>
    var jq142 = jQuery.noConflict();
</script>

回答by SRI RaMan

First Load the Older Version of Jquery.

首先加载旧版本的 Jquery。

Then your Boostrap js,css everthing goes here. Finally add this.,

然后你的 Boostrap js,css 一切都在这里。最后加上这个。,

<script type="text/javascript">
    var $versionNumberJ1 = jQuery.noConflict(true);
</script>

Then, use like this.,

然后,像这样使用。,

<script>
  $versionNumberJ1 ( function() {
    $versionNumberJ1 ( "#tabsModal" ).tabs();
  } );
  </script>