jQuery 安装jQuery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1458349/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:30:31  来源:igfitidea点击:

Installing jQuery?

jquery

提问by Hariharbalaji

What is the procedure for installing jQuery for someone new to it?

为新用户安装 jQuery 的过程是什么?

回答by meder omuraliev

Get jQuery up and running in a minute or less:

在一分钟或更短的时间内启动并运行 jQuery:

Insert this into your HTML (most commonly in the head, but you can throw it before the end body tag too):

将其插入到您的 HTML 中(最常见的是在头部,但您也可以将其放在结束 body 标记之前):

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

Then place a script element after your jQuery one. This would alert 'hello' after the DOM is ready.

然后在您的 jQuery 元素之后放置一个脚本元素。这会在 DOM 准备好后提醒“你好”。

<script>$(function() { alert('hello') });</script>

Read the documentation.

阅读文档

Using jQuery locally:

在本地使用 jQuery:

After you get a feel, try downloading jQuery locally to your computer, and link it from your script file. The structure is like so:

体验之后,尝试将 jQuery 本地下载到您的计算机,并从您的脚本文件中链接它。结构是这样的:

C:/web/index.html
C:/web/js/jquery.js

index.html:

    <head>
        <script src="js/jquery.js"></script>
        <script>$(function() { alert('hi') })</script>
    </head>

You have the advantage of relying on your saved version offline if you don't have the Internet/Wi-Fi. You can also make custom edits to the jQuery source and modify it at will.

如果您没有互联网/Wi-Fi,您可以离线依赖保存的版本。您还可以对 jQuery 源进行自定义编辑并随意修改。

Study the jQuery source [advanced]

研究 jQuery 源码 [进阶]

Download the uncompressed version from:

从以下位置下载未压缩版本:

http://code.jquery.com/jquery-latest.js

http://code.jquery.com/jquery-latest.js

After you've gained a bit of JavaScript/DOM knowledge try to take it apart step by step.

在您获得了一些 JavaScript/DOM 知识之后,尝试一步一步地将其拆开。

回答by Christian Davén

There is no installation per se.

本身没有安装。

You download jQueryand include it in your html files like this:

下载 jQuery并将其包含在您的 html 文件中,如下所示:

<script src="jquery.js" type="text/javascript"></script>

Of course, modify the filename so that it's the same as the downloaded script file.

当然,修改文件名,使其与下载的脚本文件相同。

Done!

完毕!

回答by Elitmiar

The following steps can be followed

可以按照以下步骤进行

1) Download Jquery by clicking on this link DOWNLOAD

1) 点击此链接下载 Jquery下载

2) Copy the js file into your root web directory eg. www.test.com/jquery-1.3.2.min.js

2) 将 js 文件复制到您的根 Web 目录中,例如。www.test.com/jquery-1.3.2.min.js

3) In your index.php or index.html between the head tags include the following code, and then JQuery will be installed.

3) 在你的 index.php 或 index.html 之间的 head 标签中包含以下代码,然后就会安装 JQuery。

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>

回答by Quentin

It tells you at the very start of the tutoriallinked from the jQuery homepage.

它会在从 jQuery 主页链接的教程一开始就告诉您。

回答by Macha

There is none. Use script tags to link to google's version (or download it yourself and link to your copy if you really want to).

空无一人。使用脚本标签链接到谷歌的版本(或者自己下载并链接到你的副本,如果你真的想要的话)。

If you don't know how to do that, learn HTML and Javascript first before attempting to learn jQuery.

如果您不知道该怎么做,请先学习 HTML 和 Javascript,然后再尝试学习 jQuery。

回答by rahul

There is no installation required. Just add jQuery to your application folder and give a reference to the js file.

无需安装。只需将 jQuery 添加到您的应用程序文件夹并提供对 js 文件的引用。

<script type="text/javascript" src="jQuery.js"></script>

if jQuery is in the same folder of your referenced file.

如果 jQuery 与您引用的文件位于同一文件夹中。

回答by adardesign

The best thing would be to link to the jQuery core is via google.
There are 3 reasons to do it this way,

最好的办法是通过谷歌链接到 jQuery 核心。
这样做有3个原因,

  • Decreased Latency
  • Increased parallelism
  • Better caching
  • 减少延迟
  • 增加并行度
  • 更好的缓存

      see:
      http://encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/

      见:http:
      //encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function() {

Your code here.....

  });
</script>

回答by Nate

Install JQuery with only JavaScript. This is not a very good solution if you are developing a website but it's great if you want JQuery in the JavaScript console of a random website that does not use JQuery already.

仅使用 JavaScript 安装 JQuery。如果您正在开发一个网站,这不是一个很好的解决方案,但如果您希望在一个尚未使用 JQuery 的随机网站的 JavaScript 控制台中使用 JQuery,这将是一个很好的解决方案。

function loadScript(url, callback)
{
    // adding the script tag to the head as suggested before
   var head = document.getElementsByTagName('head')[0];
   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = url;

   // then bind the event to the callback function 
   // there are several events for cross browser compatibility
   script.onreadystatechange = callback;
   script.onload = callback;

   // fire the loading
   head.appendChild(script);
}
loadScript("http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js")

loadScript function thanks to e-satis's answer to Include JavaScript file inside JavaScript file?

loadScript 函数感谢e-satis在 JavaScript 文件中包含 JavaScript 文件的回答

回答by cowgod

There are two different ways you can utilize jQuery on your website. To start off, you need to have access to your website source, whether it be straight HTML or generated HTML from a programming language. Then you need to insert a <script>tag that will render in the final output to the web browser.

您可以通过两种不同的方式在您的网站上使用 jQuery。首先,您需要访问您的网站源代码,无论是直接的 HTML 还是从编程语言生成的 HTML。然后,您需要插入一个<script>标签,该标签将在 Web 浏览器的最终输出中呈现。

Because you are new to jQuery, I highly suggest you start reading How jQuery works.

因为您是 jQuery 的新手,我强烈建议您开始阅读jQuery 的工作原理

As others have mentioned, there are Content Distribution Networks (CDNs) that host JQuery -- all you need to do is point your script tag srcto a specific URI. Googleand Microsoftboth have CDNs that are free for personal and commercial use.

正如其他人提到的,有承载 JQuery 的内容分发网络 (CDN) —— 您需要做的就是将脚本标记src指向特定的 URI。 谷歌微软都有免费供个人和商业使用的 CDN。

Alternatively, you can download jQueryand host it on your own website.

或者,您可以下载 jQuery并将其托管在您自己的网站上。

You can also leverage both of these methods together. In the event that the Google or Microsoft CDN is down or blocked in the end user's country/firewall/proxy, you can fallback to your locally hosted copy of jQuery.

您还可以同时利用这两种方法。如果 Google 或 Microsoft CDN 在最终用户的国家/地区/防火墙/代理中关闭或被阻止,您可以回退到本地托管的 jQuery 副本。

回答by Scozzard

jQuery is just a JavaScript library (simply put, a JavaScript file). All you have to do is put it into your website directory and reference it in your HTML to use it.

jQuery 只是一个 JavaScript 库(简单地说,一个 JavaScript 文件)。您所要做的就是将其放入您的网站目录并在您的 HTML 中引用它以使用它。

For example, in the head tag of your webpage

例如,在您网页的 head 标签中

<script type="text/javascript" src="../Js/jquery.js"></script>

You can download the current jQuery release from Downloading jQuery.

您可以从下载 jQuery下载当前的 jQuery 版本。