使用Modernizr,但$未定义

时间:2015-01-20 20:25:09

标签: javascript jquery modernizr

我使用的是Modernizr,并且我已经从我之前使用过的其他网站复制了我的解决方案,并且没有任何问题。

当我加载页面时,我得到ReferenceError:$未定义。 (指$(文件).ready(function(){...)

现在我知道我得到了,因为jquery没有加载,但是我无法弄清楚我在这里缺少什么来阻止jquery加载。

我错过了什么?

编辑:取出网址并在此处发布原始代码:

<script src="http://www.domain.com/js/modernizer.custom.js"></script>
<script>
//use the modernizr load to load up external scripts. This will load the scripts asynchronously, but the order listed matters. Although it will load all scripts in parallel, it will execute them in the order listed

Modernizr.load([
{
    load: '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'
},
{
    // test for media query support, if not load respond.js
    test : Modernizr.mq('only all'),
    // If not, load the respond.js file
    nope : '/js/respond.min.js' 
}
]); 
</script>

1 个答案:

答案 0 :(得分:4)

不要将Modernizer.load用于jQuery - .load函数主要用于加载polyfill和兼容性插件等。

在您对<script>进行任何引用之前,只需在自己的$标记中单独加载jQuery。

如果你想继续使用.load,你需要将你的初始jQuery相关代码移动到.load完成回调中,这样你才能在Modernizr之后尝试调用jQuery。异步加载它。