为什么我在document.write中链接我的jquery? [英] Why do I link my jquery inside a document.write?
问题描述
此代码的目的是什么:
<script>window.jQuery || document.write('<script src="jquery-1.11.3.min.js"><\/script>')</script>
而不是:
<script src="jquery-1.11.3.min.js"></script>
..将jquery链接到我的html文件时。
..when linking jquery to my html file.
这可能是一个愚蠢的问题。我是网络开发的新手。当我使用第一个代码时,我的jquery将无法工作。当我把它切换到第二个代码时,它会加载,但它是毛病。我在< / body>
之前有这段代码。任何帮助是极大的赞赏。
This might be a stupid question. I'm new to web development. My jquery won't work when I use the first code. When I cut it down to the second code, it loads but it is glitchy. I have this code just before </body>
. Any help is greatly appreciated.
推荐答案
从CDN加载jquery时通常会使用这行代码,例如
That line of code is usually used when you load jquery from a CDN, like
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script>window.jQuery || document.write('<script src="jquery-1.11.3.min.js"><\/script>')</script>
第二个脚本标签将检查是否定义了window.jQuery(这意味着脚本已成功加载CDN)。如果没有加载本地存储的版本。
The second script tag will check if window.jQuery is defined (which means the script was successfully loaded from the CDN). If not load a locally stored version.
这篇关于为什么我在document.write中链接我的jquery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!