Javascript 如何将jQuery noConflict与引导和Dojo一起使用
我已经为AMD编写了一个小部件,它使用了最新版本的jQuery、Bootstrap和Dojo。问题是,它将嵌入到带有较旧版本jQuery的页面中。(客户端页面,我无法控制) 我尝试使用jQuery的noConflict选项,但在尝试加载引导时无法使其工作 简单的无冲突工作:Javascript 如何将jQuery noConflict与引导和Dojo一起使用,javascript,dojo,twitter-bootstrap-3,js-amd,Javascript,Dojo,Twitter Bootstrap 3,Js Amd,我已经为AMD编写了一个小部件,它使用了最新版本的jQuery、Bootstrap和Dojo。问题是,它将嵌入到带有较旧版本jQuery的页面中。(客户端页面,我无法控制) 我尝试使用jQuery的noConflict选项,但在尝试加载引导时无法使其工作 简单的无冲突工作: require(["jquery"], function (jQueryForPlugin) { // create local scope var and resets the global $ and jQuery
require(["jquery"], function (jQueryForPlugin) {
// create local scope var and resets the global $ and jQuery
// to their previous values, so we don't mess up client's page
var jQuery = jQueryForPlugin.noConflict(true);
var $ = jQuery;
// Now I can use $ like normal, and it will use my widget's jQuery version.
$('.some-class').hide();
问题在于其他库,特别是Twitter引导库,当我加载它们时,它们看不到新的作用域jQuery变量
引导程序无法看到本地noConflict$和jQuery:
香草引导不兼容AMD。您必须编辑Bootstrap的JS,以便自己添加到AMD模块样板中,这样它就可以
require()
jQuery
另见
[编辑:Bootstrap v3.2.0应与AMD兼容!请参阅]
require(["jquery"], function (jQueryForPlugin) {
// Resets the global $ and jQuery to their previous values.
var jQuery = jQueryForPlugin.noConflict(true);
var $ = jQuery;
require([ 'bootstrap'], function ( bootstrap) {
// ERR: Bootstrap load throws error because it can't find 'jQuery' in its scope.