从CDN获取jQuery? [英] Sourcing jQuery from a CDN?

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

问题描述

我正在使用require JS并想知道使用CDN版本的jQuery的最佳方法。我听说1.7版本是AMD,应该有所帮助,但找不到直接的例子。希望一些RequireJS大师可以帮助我。

I am using require JS and want to know the best method to use a CDN version of jQuery. I hear the 1.7 version is "AMD" which is supposed to help but can't find a straight example. Hope some RequireJS gurus can help me out.

推荐答案

jQuery 1.7将自己注册为名为'jquery'的AMD模块,所以你需要使用路径配置为'jquery'创建一个映射:

jQuery 1.7 registers itself as an AMD module by the name of 'jquery', so you need to create a mapping for 'jquery' using the paths config:

requirejs.config({
  paths: {
    'jquery' : 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min'
  }
});

require(['jquery'], function($) {
  //$ points to jQuery
});

但是请注意,RequireJS异步加载模块而乱序,所以如果你有想要使用的jQuery插件没有包含在 define(['jquery'],函数($){/ *插件代码在这里* /}); 调用,插件可以在加载jQuery之前执行。

Note however that RequireJS loads modules asynchronously and out of order, so if you have jQuery plugins you want to use that are not wrapped in define(['jquery'], function ($){ /* plugin code goes here */ }); calls, the plugin could execute before jQuery is loaded.

参见 require-jquery 项目的自述文件,介绍如何处理依赖于jQuery的文件但不包装在 define()调用中。

See the require-jquery project's README on ways to deal with files that depend on jQuery but do not wrap themselves in define() calls.

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

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