日期:2014-05-16 浏览次数:20425 次
RequireJS是什么?可以用他来作什么?您可以继续阅读本文的参考连接,本文只为刚开始使用RequireJS来加载jQuery的朋友提供使用小提示.RequireJS的更多使用技巧可以访问其网官
A.RequireJS让我找不到北了
1.通常一个A网页引入一个B.js文件,A就可以使用B中定义的函数或变量,但为什么我下面的代码不好使呢?
<script data-main="/T/orange/images/B" src="/js/require.js"></script>
requirejs.config({ baseUrl: '/js/' //这是一个相对应用的目录,如果你写成../js/哪就是相对B.js的目录 }); require(['jquery'],function($){ //my js code });
define(["jquery"], function($){ return window.jQuery; });
B.路径不对呀?
1.这个require路径我完全糊涂了,A.html我的js代码
<script data-main="/T/orange/images/main" src="/js/require.js"></script> <script type="text/javascript"> require(['/js/jquery','require_common'],function($){ //my js code }); </script>
require(['../../../js/jquery','require_common'],function($){ //my js code });
<script type="text/javascript"> requirejs.config({ paths: { "jquery": "../../../js/jquery" } }); require(['jquery','require_common'],function($){ //my js code }); </script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.js"></script> <script type="text/javascript" src="/js/nicEdit.js"></script> <script type="text/javascript" src="/js/halo.js"></script> <script type="text/javascript" charset="UTF-8" src="/T/orange/images/require_common.js"></script>
require(['../../../js/jquery','../../../js/nicEdit','../../../js/halobox','require_common'],function($){ //my js code });
<script type="text/javascript"> requirejs.config({ paths: { "jquery": "../../../js/jquery", "halobox": "../../../js/halobox", "nicEdit":"../../../js/nicEdit", "require_common":"../templates/orange/images/require_common" }, shim:{ 'require_common':{ deps:['jquery','halobox','nicEdit'] }, 'halobox':{ deps:['jquery'], exports: 'halobox' } } }); require(['main','require_common'],function($){ //my js code alert(halobox.getShareSite()); }); </script>
点击打开链接