使用 Modernizr,但未定义 $

Using Modernizr, but $ is not defined

本文关键字:未定义 Modernizr 使用      更新时间:2023-09-26

我正在使用 Modernizr,并且我从以前使用过它的另一个站点复制了我的解决方案,并且没有任何问题。

当我加载页面时,我收到引用错误:$ 未定义。(参考 $(document).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>

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

只需在自己的 <script> 标记中独立加载 jQuery,然后再对 $ 进行任何引用。

如果你确实想继续使用 .load ,你需要将最初的 jQuery 相关代码移动到 .load 完成回调中,这样你就不会尝试调用 jQuery,直到 Modernizr 异步加载它。