HTML5 Boilerplate jQuery库后备如何工作? [英] How does HTML5 Boilerplate jQuery library fallback work?

查看:85
本文介绍了HTML5 Boilerplate jQuery库后备如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是html5样板的初学者问题,以及一般的javascript,但我似乎无法在任何地方找到答案,所以在这里......

This is a beginner question to html5 boilerplate, and javascript in general, but I can't seem to find the answer anywhere, so here it goes...

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>

第二个脚本行如何检查是否已加载Google的CDN或不?没有if声明或任何暗示。如果这听起来很愚蠢,我很抱歉。有什么帮助吗?

How does the second script line checks whether the google's CDN has been loaded or not? There's no if statement or anything to suggest that. I'm sorry if this sounds stupid. Any help?

推荐答案

|| 是这里的if语句案件。如果 window.jQuery 返回 TRUE ,那么或语句(||)不会被加载。如果它是 FALSE ,那么它将继续加载jquery。

The || is the if statement in this case. If window.jQuery returns TRUE, then anything after the or statement (||) won't get loaded. If it's FALSE, then it'll continue on to load jquery.

编辑:只是为了澄清一点。如果您在javascript中执行 if(var1&& var2),它将评估BOTH变量以检查它们是否都为真。如果你使用 if(var1 || var2),那么如果第一个变量的计算结果为TRUE,则不需要计算表达式的其余部分,因为它会自动生成无论哪种方式都是正确的。

Just to clarify a bit. If you do if (var1 && var2) in javascript, it'll evaluate BOTH variables to check if they're both true. If you make it if (var1 || var2), then if the first variable evaluates to TRUE, there's no need to evaluate the rest of the expression, since it'll automatically be true either way.

在这种情况下,这正是你的代码正在做的事情。如果 window.jQuery 为FALSE(意味着未加载jQuery),则继续并评估下一个表达式 - 在这种情况下,从本地文件加载jquery。它只是没有包含在IF语句中,因为它没有必要。

In this case, that's exactly what your code is doing. If window.jQuery is FALSE (meaning jQuery wasn't loaded), then continue on and evaluate the next expression--which in this case loads the jquery from a local file. It's just not wrapped in an IF statement since it's not necessary.

这篇关于HTML5 Boilerplate jQuery库后备如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆