[译]Pro ASP.NET MVC 3 Framework 3rd Edition (Chapter 20 JQuery) 2.Referencing jQuery 引用jQuery

来源:互联网 发布:js微信获取定位api 编辑:程序博客网 时间:2024/05/01 05:54

Every new MVC Framework project that Visual Studio creates includes the jQuery library files, which can be found in the /Scripts folder. There are a number of jQuery files, and it is important to know what each of them does, as described in Table 20-1.

每一个通过Visual Studio新建的MVC框架项目会包含jQuery库文件,你可以在“/Scripts”文件夹中找到,这里有许多jQuery文件,弄明白它们每一个能做什么是非常重要的。表20-1描述了它们的作用。

表20-1. Visual Studio MVC框架项目包含的jQuery文件(jQuery Files Included in a Visual Studio MVC Framework Project)

Library File 库文件Description 描述jquery-1.5.1.js 
jquery-1.5.1.min.js

The regular and minimized versions of the core jQuery library.标准版、简化版的jQuery核心版本库

jquery-ui.js 
jquery-ui.min.js

The regular and minimized versions of the jQuery UI library. See the “Using jQuery UI” section of this chapter for more information.

标准版、简化版的jQuery UI版本库,查看“使用jQuery UI”章节部分获取更多信息。

jquery-unobtrusive-ajax.js 
jquery-unobtrusive-ajax.min.js

The regular and minimized versions of the library that supports unobtrusive Ajax, described in Chapter 19.

标准版、简化版的支持Unobtrusive Ajax的版本库,在章节19有被提及。

jquery-validate.js 
jquery-validate.min.js

The regular and minimized versions of the unobtrusive client-side validation feature, described in Chapter 18.

标准版、简化版的支持Unobtrusive(非强制)客户端验证特性的版本库,在章节18有被提及。

jQuery-1.5.1-vsdoc.js 
jQuery-validate-vsdoc.js

IntelliSense support for the core and validation libraries. See the “Writing jQuery Code” section of this chapter for details. 核心、验证库的智能感知支持,查看“编写jQuery 代码”章节获取详细信息。

Two versions of each file are available: the regular and minimized versions. The regular versions are human-readable and are useful during development. When we have a problem, we can use the JavaScript debugger in our browser and see what’s going on. They are also useful for simply learning about jQuery and JavaScript in general.

每一个文件都有两个版本可供选择:标准版和简化版。标准版是可读的,在开发期间非常有用,当我们遇到一个问题,我们可以使用Javascript 调试器在我们浏览器中查看发生了什么事情。他们还使学习jQuery和常规的JavaScript变得更简单 。

Tip :There are also a set of files in the ~/Scripts folder whose names start with Microsoft, for example MicrosoftAjax.js. These are from version 2 of the MVC Framework and predate Microsoft fully embracing jQuery in ASP.NET MVC. We don’t discuss these files since they have been superseded and are included in MVC Framework just for compatibility with earlier versions.

提醒:在“~/Scripts ”文件夹中也存在一组以“Microsoft”开头的文件,例如:“MicrosoftAjax.js”。这些文件来自MVC 框架的第二版,是微软早期包含在ASP.net中的jQuery。我们不讨论这些已经被取代的文件,他们的存在只是为了兼容早期的版本。

The minimized files contain the same JavaScript code but processed to reduce the size of the files that the browser has to download. Comments, long variable names, and unnecessary whitespace are all removed. It may not sound like much, but minimized files can be significantly smaller than their regular counterparts.

简化版的文件包含了相同的JavaScript代码,但是为了能方便浏览器下载而被缩小了文件的尺寸。注释、长变量名和一些空格被移除。这听起来不太可能,但最小化文件看起来明显小于标准文件。

                       MANAGING J QUERY VERSIONS  管理jQuery版本                                                    

As we write this, Visual Studio creates MVC Framework projects using jQuery 1.5.1. The MVC Framework is released on a different schedule than jQuery. The current version is 1.6.1, but it will almost certainly have changed again by the time you are reading this book. If you want to update the version of jQuery, simply open the Package Manager console (from the Tools ➤ Library Package Manager menu), and enter the following command: Update-Package jquery

当我们写到这里,Visual Studio创建的MVC框架项目采用的是jQuery1.5.1版本。MVC框架的发布计划不同于jQuery。jQuery的当前版本是1.6.1,但当你阅读这本书的时候,它几乎肯定已经改变了。如果你想升级jQuery的版本,最简单的方法就是打开Package Manager控制台(在“工具>Library Package Manager”菜单中),然后输入以下的命令:Update-Package jquery

This will remove your project’s existing jQuery script files and replace them with the latest versions. You will then need to update /Views/Shared/ Layout.cshtml so that it references the newly added files, for example changing the following:

这将会移除项目中已经存在的jQuery脚本文件,用最新的版本替代。这个时候,你需要更新“/Views/Shared/ _Layout.cshtml ”文件使它引用新添加的文件,例如改变如下:

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" ...

to this:

变更为:

<script src="@Url.Content("~/Scripts/jquery-1.6.1.min.js")" ...

We give more details on how to reference the jQuery libraries shortly.

不久我们就会给出如何引用jQuery库的更多细节.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

New ASP.NET MVC 3 projects include by default a reference to the jQuery library in the default layout file, ~/Views/Shared/ Layout.cshtml, as shown in Listing 20-3.

新的ASP.NET MVC3项目默认会在项目默认的布局文件”~/Views/Shared/ _Layout.cshtml”中引用jQuery库文件,如同清单20-3中显示:

清单20-3. jQuery在_Layout.cshtml中的引用(The jQuery Reference in _Layout.cshtml)

<!DOCTYPE html> <html> <head>     <title>@ViewBag.Title</title>     <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />     <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")"          type="text/javascript"></script> </head>  <body>     @RenderBody() </body> </html> 

If we want to use jQuery in a view that does not use the default layout, then we need to make sure to copy the script element to the layout that the view does use, or directly into the view itself.

如果我们想在一个不使用默认布局方式的视图中使用jQuery的话,我们需要确定将这脚本元素拷贝到这个视图所使用的布局文件中,或者直接拷贝到视图本身。

■ Tip: For Internet applications, it can make sense to obtain the jQuery library from a content distribution network (CDN). See the “Using a CDN for JavaScript Libraries” sidebar in Chapter 18 for details.

■提醒:对于Internet应用,能从内容分布网络(CDN)得到jQuery库是很有意义的。在章节18中查看“为JavaScript库使用CDN”栏获取详细信息。

原创粉丝点击