通过 Modernizer 加载 jQuery 时 $ 未定义。
我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
yepnope/modernizr.load 中的脚本是异步加载的。这意味着根据设计,它们不会在其包含的正下方的空间中执行。这不会阻止页面进一步呈现,从而提高页面性能。
callback
和complete
选项可供您在脚本准备就绪时收到提醒。确保 jQuery 存在的最快方法是将内容包装在页面下方:然后在
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
andcomplete
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:And then in the
complete
function, call theappInit()
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.
问题的出现是因为您的 Modernizr 正在加载 jQuery 并像这样分配它: $ = jQuery,但是,它仅在
complete: function() {
上下文中分配。为了使其在该上下文之外工作,在
Modernizer.load
设置之前:这会将
$
的范围设置为全局,以便您可以在此时的任何地方使用它。如果你没有将
$
的范围设置为全局,使用 jQuery 仍然会像这样工作: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: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: