繁体   English   中英

Require.js Uncaught TypeError:undefined不是函数

[英]Require.js Uncaught TypeError: undefined is not a function

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

现在main.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更改为

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

我明白问题是它没有加载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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM