通过 Modernizer 加载 jQuery 时 $ 未定义。

发布于 2024-12-26 10:43:15 字数 1005 浏览 4 评论 0原文

我使用 Modernizr 加载 jQuery,“完整”函数中的所有代码都运行良好!但是如果我尝试从“Moderniz.load”外部调用一些js,萤火虫会说:“$未定义”。

这有效:

<script>
Modernizr.load([
{
    load: [ '//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js'],
    complete: function () {
      if ( !window.jQuery ) {
            Modernizr.load('/weblounge-sites/www/js/jquery-1.7.min.js', '/weblounge-sites/www/js/jqueryui-1.8.min.js');
      }
    }
},
{
    load: [ 'some additional scripts' ],
    complete: function() {
        $ = jQuery;
        $(document).ready(function(){
          some js
          });

        });
    }
},  
{
    test: Modernizr.boxshadow,
    nope: 'polyfills/PIE.js',
}
]);
</script>

但是稍后从视图行调用失败:

<script>
$(document).ready(function(){
    $('#hauptsponsoren').cycle({
        fx: 'fade', 
        speed: 4000,
        timeout: 10000
    });                 
});
</script>

I load jQuery with modernizr and all code in the 'complete'-function runs fine! But if I try to call some js from outside 'Moderniz.load' firebug says: '$ is not defined'.

This works:

<script>
Modernizr.load([
{
    load: [ '//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js'],
    complete: function () {
      if ( !window.jQuery ) {
            Modernizr.load('/weblounge-sites/www/js/jquery-1.7.min.js', '/weblounge-sites/www/js/jqueryui-1.8.min.js');
      }
    }
},
{
    load: [ 'some additional scripts' ],
    complete: function() {
        $ = jQuery;
        $(document).ready(function(){
          some js
          });

        });
    }
},  
{
    test: Modernizr.boxshadow,
    nope: 'polyfills/PIE.js',
}
]);
</script>

But the call from a view lines later fails:

<script>
$(document).ready(function(){
    $('#hauptsponsoren').cycle({
        fx: 'fade', 
        speed: 4000,
        timeout: 10000
    });                 
});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┈┾☆殇 2025-01-02 10:43:15

yepnope/modernizr.load 中的脚本是异步加载的。这意味着根据设计,它们不会在其包含的正下方的空间中执行。这不会阻止页面进一步呈现,从而提高页面性能。

callbackcomplete 选项可供您在脚本准备就绪时收到提醒。确保 jQuery 存在的最快方法是将内容包装在页面下方:

<script>
function appInit() {
  $(document).ready(function(){
    $('#hauptsponsoren').cycle({
      fx: 'fade', 
      speed: 4000,
      timeout: 10000
    });                 
  });
}
</script> 

然后在 complete 函数中,调用 appInit() 函数。您的页面加载时间将会感谢您。

完成: function() { ...;应用程序初始化(); 。

如果此时你的 dom 已准备好,它将立即运行,或者如果尚未准备好,它也会等待一段时间才能发生

希望能解决这个问题。

Scripts in yepnope/modernizr.load are loaded asynchronously. That means that by design, they won't have executed in the space directly below their inclusion. This makes the page performance better by not blocking the page from rendering further.

The callback and complete options are there for you to be alerted whenever the scripts are ready. The fastest route to making sure that jQuery exists, is to just wrap the stuff lower on your page:

<script>
function appInit() {
  $(document).ready(function(){
    $('#hauptsponsoren').cycle({
      fx: 'fade', 
      speed: 4000,
      timeout: 10000
    });                 
  });
}
</script> 

And then in the complete function, call the appInit() function. Your page load times will thank you.

complete: function() { ...; appInit(); }

If your dom is ready at that point, it will run right away, or if it's not yet, it will wait a little longer for that to occur as well.

Hope that clears it up.

述情 2025-01-02 10:43:15

问题的出现是因为您的 Modernizr 正在加载 jQuery 并像这样分配它: $ = jQuery,但是,它仅在 complete: function() { 上下文中分配。
为了使其在该上下文之外工作,在 Modernizer.load 设置之前:

var $;

这会将 $ 的范围设置为全局,以便您可以在此时的任何地方使用它。

如果你没有将 $ 的范围设置为全局,使用 jQuery 仍然会像这样工作:

jQuery(document).ready(function()

The problem comes because your modernizr is loading jQuery and assigning it like this: $ = jQuery, however, it's only assigned within the complete: function() { context.
To make it work outside of that context, before your Modernizer.load set:

var $;

That will set the scope of $ as global so you can use it anywhere at that point.

If you don't set the scope of $ to global, using jQuery will still work like this:

jQuery(document).ready(function()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文