最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Require.js Uncaught TypeError: undefined is not a function - Stack Overflow

programmeradmin0浏览0评论

I just started learning Require.js

the file system is like :

This is my 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>

Now, this I knows that the 1st file that loaded on to the DOM is require.js then after that it loads lib/main.js

Now the main.js is

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

Now here is the problem if I keep jquery.js in the same folder as that in main.js the code works fine, but if I change the path to jquery/jquery.js and change the main.js as

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

I understood the problem is that it is not loading the jquery.js if it is inside any other folder other that main.js is, but why, please shed some light and how that can be achieved.

I just started learning Require.js

the file system is like :

This is my 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>

Now, this I knows that the 1st file that loaded on to the DOM is require.js then after that it loads lib/main.js

Now the main.js is

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

Now here is the problem if I keep jquery.js in the same folder as that in main.js the code works fine, but if I change the path to jquery/jquery.js and change the main.js as

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

I understood the problem is that it is not loading the jquery.js if it is inside any other folder other that main.js is, but why, please shed some light and how that can be achieved.

Share Improve this question asked Apr 12, 2013 at 12:25 Ankur VermaAnkur Verma 5,93316 gold badges61 silver badges95 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

To use RequireJS and jQuery, you should either use the bined RequireJS/jQuery file, available at:

http://requirejs/docs/jquery.html

Or use a path.

http://requirejs/docs/api.html#config-paths

require.config({
    paths: {
        "jquery": 'http://code.jquery./jquery-1.9.1'
    }
});

require(["jquery"], function ($) {
    console.log($.fn.jquery);
});
发布评论

评论列表(0)

  1. 暂无评论