【问题标题】:Require.js Uncaught TypeError: undefined is not a functionRequire.js Uncaught TypeError: undefined is not a function
【发布时间】:2013-04-12 20:57:17
【问题描述】:

刚开始学习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>

现在,我知道加载到 DOM 的 第一个 文件是 require.js,然后加载 lib/main.js

现在 ma​​in.js

require(['jquery'],function($){
     //this works since both **main.js** and **jquery.js** are in same folder
     $("#content").html("jquery am  loaded");
});

现在,如果我将 jquery.js 保存在与 main.js 相同的文件夹中,那么问题就来了,代码可以正常工作,但是如果我将路径更改为 jquery/jquery.js 并更改 main.js。 js作为

require(['jquery/jquery'],function($){
     //this thing shows 'Uncaught TypeError: undefined is not a function'
     $("#content").html("jquery am  loaded");
});

我理解问题在于,如果 jquery.js 位于 main.js 以外的任何其他文件夹中,它不会加载它是,但是为什么,请阐明一下如何实现。

【问题讨论】:

    标签: javascript jquery requirejs


    【解决方案1】:

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

    【讨论】:

    • 最近我使用了你的代码,看到 jquery 文件正在加载 Network Inspector 但我仍然很困惑为什么 require.js 不是在回调的 $ 中推入参数。
    • 不推$参数在哪个回调中?
    • require(['jquery/jquery'],function($){ //这里$是jquery的内容推送或者放置的参数吧? $("#content" ).html("jquery 已加载"); });
    • 使用./jquery/jquery 怎么样(if jquery/jquery 相对于main.js)。请注意,这不是正确的方法。
    猜你喜欢
    • 1970-01-01
    • 2014-08-15
    • 2015-01-14
    • 2014-07-06
    • 2014-09-01
    • 2015-11-04
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多