RequireJS不同的jQuery [英] RequireJS different jQuery

查看:63
本文介绍了RequireJS不同的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用RequireJS加载jQuery(我开发的模块应该在可能已经初始化jQuery的不受控制的环境中工作),但是当需要不同版本的jQuery时会出现一些问题.结果是无法预期的.里面的require功能块jQuery版本是随机的.怎么了? 这是说明问题的代码:

I would like to use RequireJS to load jQuery (module I develop supposed to work in uncontrolled environment where jQuery might be already initialized), but there are some problems when require different versions of jQuery. Results are unexpectable. Inside require function block jQuery version is kind of random. What is wrong? Here is code to illustrate problem:

<!DOCTYPE html>
<html>
<head>
    <script data-main="scripts/main" src="http://ajax.cdnjs.com/ajax/libs/require.js/0.24.0/require.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
</head>
<body>

<script>
    console.log(jQuery.fn.jquery);

    setTimeout(function(){
      require(["http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"], function() {
      console.log($.fn.jquery);
      });
    },1000);
    setTimeout(function(){
      require(["http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"], function() {
      console.log($.fn.jquery);
      });
    },2000);

    setTimeout(function(){
      require(["http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.js"], function() {
      console.log($.fn.jquery);
      });
    },3000);

    setTimeout(function(){
      require(["http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"], function() {
      console.log($.fn.jquery);
      });
    },4000);

    setTimeout(function(){
      require(["http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"], function() {
      console.log($.fn.jquery);
      });
    },5000);

    setTimeout(function(){
      require(["http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"], function() {
      console.log($.fn.jquery);
      });
    },6000);

    setTimeout(function(){
      require(["http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"], function() {
      console.log($.fn.jquery);
      });
    },7000);

    setTimeout(function(){
      require(["http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.js"], function() {
      console.log($.fn.jquery);
      });
    },8000);

    setTimeout(function(){
      require(["http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"], function() {
      console.log($.fn.jquery);
      });
    },9000);
</script>
</body>
</html>

结果是:

1.6.2
1.8.0
1.4.4
1.8.0
1.8.0
1.6.2
1.6.2
1.6.2
1.6.2
1.6.2

推荐答案

假设您使用的是 AMD(异步兼容版本的jQuery,应将jQuery对象用作该函数的第一个参数:

Assuming you are using AMD (Asynchronous Module Definition) compatible versions of jQuery, the jQuery object should be made available as the first argument of the function :


require(["some_version_of_jquery.js"], function(jQuery) {
      console.log(jQuery.fn.jquery);
      });

因此,函数内部可用的jQuery对象是在局部范围内定义的,而不是在全局范围内定义的.

So the jQuery object available inside the function is defined in local scope rather than the globally defined one.

如果您使用的不是AMD兼容版本,则必须使用 RequireJS Shim配置.

If you are not using AMD compatible versions, then you will have to use RequireJS Shim config.

这篇关于RequireJS不同的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆