在没有 Internet 连接时使用 jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15872414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Working with jQuery when there is no Internet connection
提问by JavaCoding
I am completely new to jQuery. When working with jQuery, I disconnected the Internet to check if my webpage works fine without an Internet connection. It displayed some thing which is not required. When I saw the source code, it displayed:
我对 jQuery 完全陌生。使用 jQuery 时,我断开了 Internet 连接以检查我的网页在没有 Internet 连接的情况下是否可以正常工作。它显示了一些不需要的东西。当我看到源代码时,它显示:
<link rel="stylesheet" href="http://jquery.com/jquery-wp-content/themes/jquery/css/base.css?v=1">
So I downloaded the code and kept it in my root folder. Still it's not working well. I wanted to know whether we can work with jQuery whilst offline?
所以我下载了代码并将其保存在我的根文件夹中。它仍然不能很好地工作。我想知道我们是否可以在离线时使用 jQuery?
Here is my code:
这是我的代码:
<!doctype html>
<!--[if IE 7 ]> <html class="no-js ie ie7 lte7 lte8 lte9" lang="en-US"> <![endif]-->
<!--[if IE 8 ]> <html class="no-js ie ie8 lte8 lte9" lang="en-US"> <![endif]-->
<!--[if IE 9 ]> <html class="no-js ie ie9 lte9>" lang="en-US"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title> Home</title>
<meta name="author" content="jQuery Foundation - jquery.org">
<meta name="description" content="jQuery: The Write Less, Do More, JavaScript Library">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="http://jquery.com/jquery-wp-content/themes/jquery/css/base.css?v=1">
<script src="jquery.min.js"></script>
<!--[if lt IE 7]><link rel="stylesheet" href="css/font-awesome-ie7.min.css"><![endif]-->
<script>try{Typekit.load();}catch(e){}</script>
<meta name="generator" content="WordPress 3.5.1" />
</head>
<body >
<!--[if lt IE 7]>
<p class="chromeframe">You are using an outdated browser.
<a href="http://browsehappy.com/">Upgrade your browser today</a>
or <a href="http://www.google.com/chromeframe/?redirect=true">install
Google Chrome Frame</a> to better experience this site.</p>
<![endif]-->
<header>
<section id="global-nav">
<nav>
<div class="constrain">
<ul class="links">
<li><a href="home.jsp">Home</a></li>
<li class="dropdown"><a href="CreatPatient.jsp">Patient</a>
<ul>
<li><a href="CreatePatient.jsp">Create Patient</a></li>
<li><a href="Edit Patient">Edit Patient</a></li>
</ul>
</li>
<li class="dropdown"><a href="Appointments.jsp">Appointments</a>
<ul>
<li><a href="CreateAppointments.jsp">Create Appointments</a></li>
<li><a href="EditAppointments.jsp/">Edit Appointments</a></li>
</ul>
</li>
<li class="dropdown"><a href="#">Reports</a>
<ul>
<li><a href="PreOperative.jsp">Pre Operative</a></li>
<li><a href="IntraOperative.jsp">Intra Operative</a></li>
<li><a href="PostOperative.jsp">PostOperative</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</section>
</header>
</body>
</html>
When I remove this line:
当我删除这一行时:
<link rel="stylesheet" href="http://jquery.com/jquery-wp-content/themes/jquery/css/base.css?v=1">
It creates a problem.
它会产生问题。
I have downloaded jQuery and kept on the desktop. The HTML file is also on the desktop. Please tell me why it's not working.
我已经下载了 jQuery 并保存在桌面上。HTML 文件也在桌面上。请告诉我为什么它不起作用。
回答by Albert Xing
Yes, it's possible. You've mentioned that you downloaded the file - that's a good first step, but you also have to change all the href
and src
references.
是的,这是可能的。您已经提到您下载了该文件 - 这是一个很好的第一步,但您还必须更改所有href
和src
引用。
For example,
例如,
<link rel="stylesheet" href="http://jquery.com/jquery-wp-content/themes/jquery/css/base.css?v=1">
should become
应该成为
<link rel="stylesheet" href="base.css">
Also remember to get the offline version of the jQuery JS library, too:
Download jquery.js, put it in your root folder & reference it:
还记得获取 jQuery JS 库的离线版本:
下载jquery.js,将它放在您的根文件夹中并引用它:
<script src="jquery-1.9.1.min.js"></script>
And if you want it in a subdirectory:
如果你想把它放在子目录中:
<script src="[subdirectory-name]/jquery-1.9.1.min.js"></script>
Remember that both filesneed to be offline and in your working local directory. This means that you cannot remove the stylesheet northe jQuery JS library. Keep both of them, in the local formats I've mentioned above.
请记住,这两个文件都需要脱机并位于您的工作本地目录中。这意味着您不能删除样式表或jQuery JS 库。将它们都保留在我上面提到的本地格式中。
Also, putting the <script>
tag in the <head>
is bad practice; move it just before the </body>
tag instead.
此外,将<script>
标签放入<head>
是不好的做法;将其移动到</body>
标签之前。
Your code should now look (in short, note that I haven't put much) like:
你的代码现在应该看起来(简而言之,注意我没有放太多)像:
...
<html class="no-js" lang="en-US">
<head>
...
<link rel="stylesheet" href="base.css">
...
</head>
<body>
...
<script src="jquery.min.js"></script>
</body>
</html>
Again, make sure that base.css
and jquery.min.js
are the exact file namesand are in the same folder as this .html
file
再次确保base.css
和jquery.min.js
是确切的文件名并且与此.html
文件位于同一文件夹中
回答by iConnor
Download jquery and link to it like so
下载 jquery 并像这样链接到它
<script src="js/jquery.js"></script>
<link rel="stylesheet" href="css/jquery.css">
回答by Sandeep
It will not work if you don't have internet connection. To work locally you should copy jquery.js to your local directory and give relative path.
如果您没有互联网连接,它将无法工作。要在本地工作,您应该将 jquery.js 复制到本地目录并提供相对路径。
<script src="/jquery/1.5/jquery.min.js"></script>
回答by Neophile
To work offline with jquery just copy the jquery.js file and place it in your local directory and in the script tag change the src
要离线使用 jquery,只需复制 jquery.js 文件并将其放在本地目录中,然后在脚本标记中更改 src
<script src="/path/jquery.min.js"></script>
回答by Umer Farooq
The most solution will be to serve the files via localhost. This post explains this in detail.
大多数解决方案是通过本地主机提供文件。这篇文章详细解释了这一点。
http://www.wingoku.com/2016/02/accessing-javascriptjquery-script-files.html
http://www.wingoku.com/2016/02/accessing-javascriptjquery-script-files.html
回答by Daniel
Try this source: http://www.initializr.com/
试试这个来源:http: //www.initializr.com/
It's an HTML5 templates generator to help you getting started with a new project. There is a script which loads the web jquery file if there is internet connection. If there is no Internet Connection it takes the local one.
它是一个 HTML5 模板生成器,可帮助您开始一个新项目。如果有互联网连接,有一个脚本可以加载 web jquery 文件。如果没有 Internet 连接,则使用本地连接。
Cheers.
干杯。