如何在 ASP.Net 项目中包含 jQuery?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2364734/
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
How to include jQuery in ASP.Net project?
提问by nw.
I've read that Microsoft now bundles jQuery with Visual Studio. How then do I "include" the jQuery source in my ASP.Net project?
我读过微软现在将jQuery 与 Visual Studio 捆绑在一起。那么如何在我的 ASP.Net 项目中“包含”jQuery 源代码?
回答by Nick Craver
You can include the script file directly in your page/master page, etc using:
您可以使用以下方法将脚本文件直接包含在您的页面/母版页等中:
<script type="text/javascript" src="/scripts/jquery.min.js"></script>
Us use a Content Delivery network like Google or Microsoft:
我们使用 Google 或 Microsoft 等内容交付网络:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
or:
或者:
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript"></script>
回答by ???? ?????
There are actually a few ways this can be done:
实际上有几种方法可以做到这一点:
1: Download
1:下载
You can download the latest version of jQueryand then include it in your page with a standard HTML script tag. This can be done within the master or an individual page.
您可以下载最新版本的 jQuery,然后使用标准的 HTML 脚本标记将其包含在您的页面中。这可以在母版或单个页面中完成。
HTML5
HTML5
<script src="/scripts/jquery-2.1.0.min.js"></script>
HTML4
HTML4
<script src="/scripts/jquery-2.1.0.min.js" type="text/javascript"></script>
2: Content Delivery Network
2:内容交付网络
You can include jQuery to your site using a CDN(Content Delivery Network) such as Google's. This should help reduce page load times if the user has already visited a site using the same version from the same CDN.
您可以使用CDN(内容交付网络)(例如 Google 的)将 jQuery 包含到您的站点中。如果用户已经使用来自同一个 CDN 的相同版本访问了一个站点,这应该有助于减少页面加载时间。
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
3: NuGet Package Manager
3:NuGet 包管理器
Lastly, (my preferred) use NuGetwhich is shipped with Visual Studio and Visual Studio Express. This is accessed from right-clicking on your project and clicking Manage NuGet Packages.
最后,(我的首选)使用Visual Studio 和 Visual Studio Express 附带的NuGet。这可以通过右键单击您的项目并单击Manage NuGet Packages 来访问。
NuGet is an open source Library Package Manager that comes as a Visual Studio extension and that makes it very easy to add, remove, and update external libraries in your Visual Studio projects and websites. Beginning ASP.NET 4.5 in C# and VB.NET, WROX, 2013
NuGet 是一个开源库包管理器,它作为 Visual Studio 扩展提供,可以非常轻松地在 Visual Studio 项目和网站中添加、删除和更新外部库。 在 C# 和 VB.NET 中开始使用 ASP.NET 4.5,WROX,2013
Once installed, a new Folder group will appear in your Solution Explorer called Scripts
. Simply drag and drop the file you wish to include onto your page of choice.
安装后,一个新的文件夹组将出现在您的解决方案资源管理器中,名为Scripts
. 只需将您希望包含的文件拖放到您选择的页面上即可。
This method is ideal for larger projects because if you choose to remove the files, or change versions later (though the package manager) if will automatically remove/update any reference to that file within your project.
此方法非常适用于较大的项目,因为如果您选择删除文件或稍后更改版本(通过包管理器),如果将自动删除/更新项目中对该文件的任何引用。
The only downside to this approach is it does not use a CDN to host the file so page load time may be slightly slower the first time the user visits your site.
这种方法的唯一缺点是它不使用 CDN 来托管文件,因此用户第一次访问您的网站时页面加载时间可能会稍慢。
回答by Matt Dearing
You might be looking for this Microsoft Ajax Content Delivery NetworkSo you could just add
您可能正在寻找这个Microsoft Ajax Content Delivery Network所以您可以添加
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
To your aspx page.
到您的 aspx 页面。
回答by David Fox
if you build an MVC project, its included by default. otherwise, what Nick said.
如果你构建一个 MVC 项目,它默认包含在内。否则,尼克所说的。