【译著】20章 jQuery — 《精通ASP.NET MVC 3框架》
<div id="cnblogs_post_body">说明1:本书的文字翻译工作已经完成,目前正处于出版社审核阶段,具体出版日期已经不是我决定的事情了。感谢园友对此书的广泛关注,这里贴出第20章内容。说明2:本园博主牧童先生已经贴出了本章的大部分内容(参见这里),个人认为,他的翻译是很准确的。
C H A P T E R 20
■ ■ ■
jQuery
Write less, do more—that’s the core promise of jQuery, a free, open source JavaScript library first released in 2006. It has won kudos from web developers on all platforms because of the way it cuts out the pain of client-side coding. It provides an elegant CSS 3–based syntax for traversing your DOM, a fluent API for manipulating and animating DOM elements, and extremely concise wrappers for Ajax calls—all carefully abstracted to eliminate cross-browser differences.
写得少、做得多 — 这是jQuery的核心承诺,它是2006年发布的一个免费开源的JavaScript库。由于它消除了客户端编码的痛苦,已经在所有平台的web开发者中赢得了很高的声誉。jQuery提供了一个基于CSS 3的优雅语法来遍历DOM、一个流畅的API来操纵和活跃DOM元素、以及非常简练的封装程序进行Ajax调用 — 所有这些都作了精心提炼,以消除跨浏览器的差异。
Microsoft has embraced jQuery and integrated it into the MVC Framework. When we looked at unobtrusive client validation and Ajax, it was jQuery that was doing all the hard work behind the scenes.
微软已经采纳了jQuery,并将其集成到了MVC框架中。当我们查看非唐突客户端验证和Ajax效果时,其实正是jQuery在幕后辛勤劳作。
jQuery is extensible, has a rich ecosystem of free plug-ins, and encourages a coding style that retains basic functionality when JavaScript isn’t available. We won’t claim it makes all client-side coding easy, but it is usually far easier than raw JavaScript, and it works great with the MVC Framework. In this chapter, we’ll show you the basic theory of jQuery and how to use it to add some sparkle to an MVC Framework application.
jQuery是可扩展的、具有丰富的免费插件、并且鼓励在JavaScript失效时采用一种保留基本功能的编码风格。我们并非声称它能使所有客户端编码简易,但它确实比原先的JavaScript容易许多,而且它与MVC框架配合得很好。在本章中,我们将展示jQuery的基本理论,以及如何用它把一些闪光的东西添加到MVC框架的应用程序上。
In a book about the MVC Framework, there is a limit to how much detail we can provide about jQuery. If you want to go beyond the examples we provide in this chapter (and we suggest you do), then you can find a wealth of information about jQuery at www.jquery.com. We recommend the book jQuery in Action, by Bear Bibeault and Yehuda Katz, published by Manning.
在一本关于MVC框架的书中,我们不可能提供jQuery的很多细节。如果你想进入本章所提供的示例之外的领域(而且我们建议你这样做),那么你可以在www.jquery.com上找到关于jQuery的丰富信息。我们也推荐《jQuery in Action》这本书,由Bear Bibeault和Yehuda Katz所著,Manning出版。
Creating the Project
创建项目
To demonstrate the key jQuery features, we have created a simple MVC Framework application that lists mountain summits and their heights. Given that jQuery is a client-side technology, we will focus on the Razor view and HTML that this application generates. Listing 20-1 shows the view.
为了演示jQuery关键特性,我们创建了一个简单的MVC框架应用程序,它列出山峰及其高度。考虑到jQuery是一项客户端技术,因此我们将把注意力集中在Razor视图、以及应用程序所生成的HTML上。清单20-1演示了这个视图。
<div class="code">Listing 20-1. The Sample Application Index.cshtml View
清单20-1. 示例应用程序的Index.cshtml视图
@using MvcApp.Models;@model IEnumerable<Summit>@{ ViewBag.Title = "List of Summits";}<h4>Summits</h4><table> <thead> <tr><th>Name</th><th>Height</th><th></th></tr> </thead> @foreach (Summit s in Model) { <tr> <td>@s.Name</td> <td>@s.Height</td> <td> @using (Html.BeginForm("DeleteSummit", "Home")) { @Html.Hidden("name", @s.Name) <input type="submit" value="Delete" /> } </td> </tr> }</table>@Html.ActionLink("Add", "AddSummit")@using (Html.BeginForm("ResetSummits", "Home")) { <input type="submit" value="Reset" />}
页:
[1]