Ank*_*rma 4 javascript jquery requirejs
我刚开始学习Require.js
该文件系统是这样的:

这是我的index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="lib/require/require.min.js" data-main="lib/main"></script>
</head>
<body>
<span id="content"></span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
现在,我知道加载到DOM 的第一个文件是require.js然后加载lib/main.js
现在main.js是
require(['jquery'],function($){
//this works since both **main.js** and **jquery.js** are in same folder
$("#content").html("jquery am loaded");
});
Run Code Online (Sandbox Code Playgroud)
现在问题是如果我将jquery.js保存在与main.js相同的文件夹中,代码工作正常,但是如果我将路径更改为jquery/jquery.js并将main.js更改为
require(['jquery/jquery'],function($){
//this thing shows 'Uncaught TypeError: undefined is not a function'
$("#content").html("jquery am loaded");
});
Run Code Online (Sandbox Code Playgroud)
我明白问题是它没有加载jquery.js,如果它在main.js以外的任何其他文件夹内,但是为什么,请说明一下如何实现.
要使用RequireJS和jQuery,您应该使用组合的RequireJS/jQuery文件,该文件位于:
http://requirejs.org/docs/jquery.html
或者使用path.
http://requirejs.org/docs/api.html#config-paths
require.config({
paths: {
"jquery": 'http://code.jquery.com/jquery-1.9.1'
}
});
require(["jquery"], function ($) {
console.log($.fn.jquery);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9623 次 |
| 最近记录: |