2013-04-12 101 views
4

我刚开始学习Require.jsRequire.js遗漏的类型错误:未定义是不是一个函数

文件系统是这样的:

enter image description here

这是我的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> 

现在,我知道文件加载到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"); 
}); 

enter image description here 现在,这里是问题如果我保持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"); 
}); 

我理解的问题是,它不加载的jquery.js如果是其他的任何其他文件夹里面那个main.js是,但为什么,请提供一些线索,以及如何能取得成就。

回答

7

要使用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); 
}); 
+0

最近,我用你的代码,看到jQuery的文件加载在**网络督察**,但我仍然感到困惑,为什么** ** require.js不推参数在** $ **的回调中。 –

+0

不推送$参数的回调? –

+0

require(['jquery/jquery'],function($){//这里$是推送或放置的jquery内容的参数, jquery am loaded“); }); –

相关问题