




免费预览已结束,剩余5页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
外文资料及中文译文院系名称 信息学院 专业班级 _ 计科08-3 9外文资料出处:/ajax-jqueryEasy Ajax with jQuery ArticleAjax is changing web applications, giving them a responsiveness thats unheard of beyond the desktop. But behind all the hype, theres not much to Ajax (X)HTML, JavaScript, and XML are nothing new, and in this tutorial, Ill show you how to simplify the process of adding Ajax to your application even further with the help of jQuery, a popular JavaScript library.Whats Ajax?Youve probably heard about Ajax before, or at least used an Ajax-based application Gmail, for instance. Quite simply, Ajax is a technique for handling external data through JavaScript asynchronously, without reloading the entire page. Site Point offers a good introduction to Ajax. Jesse James Garrett is credited with coining the term in this article.Unfortunately, in-depth tutorials on practical ways to enter the world of Ajax are few and far between. To add to the problem, the XML Http Request class used by Ajax isnt very easy for beginning web developers to use. Luckily, a number of JavaScript libraries offer an easier way. Today Ill show you how j Query one of these libraries allows you to easily add Ajax to your application.Whats jQuery?jQuery is another mature JavaScript library that offers some features that the others do not. Admittedly, its not exactly as lightweight as some of the other offerings: jQuery comes in at 19kb, while moo.fx is only 3kb. You can read more about jQuery at The JavaScript Library World Cup for a comparison of a few other JavaScript libraries that offer similar functionality.Assumed KnowledgeTo complete this tutorial, youll need some basic JavaScript knowledge. If you know any C-style languages, youll get the hang of JavaScript in no time. Just think curly braces, function declarations, and optional semicolons at the end of each line (theyre not optional with jQuery, though). If youre keen to get started with JavaScript, see this excellent, concise JavaScript tutorial designed for programmers. Also, since were talking about web applications, a basic knowledge of HTML is required.jQuery 101Lets walk through a quick introduction to jQuery. To be able to use it in your pages, youll first need to download the library. You can download the latest version 1.1.2 at the time of writing. jQuerys methodology is simple: find things, do stuff. We select elements from the document (via the DOM) using the jQuery function, aliased as $(). This handy function acts just like document.getElementById(), except that instead of only supporting IDs, it supports CSS selectors and some XPath selectors; and, instead of returning one element, it can return an array of elements. Okay, so maybe a better description of $() is that its like document.getElementById() on steroids.We then use functions to perform actions on our selections. For example, to append the text “Hello World!” to all divs with the class foo, then set the color to red, wed use the following code:$(div.foo).append(Hello World!).css(color,red);Easy! Normally, this task would require two lines of code, like so:$(div.foo).append(Hello World!);$(div.foo).css(color,red);jQuerys chainable methods allow us to write much more compact code than other JavaScript libraries. There are functions in jQuery that dont need an object, as they work independently, and many of the Ajax functions fall into this group. For example, the post function, which we will soon make use of, is called by typing $.post(parameters). For more jQuery functions, check the online documentation or .Example 1 Our First Ajax ApplicationAs an example, were going to make an interactive concept generator. Basically, this involves our selecting two terms at random from a list, then combining them to create a phrase. For this exercise, well use web 2.0 buzzwords (Mashup, Folksonomy, Media and so on), and normally wed fetch these terms from a flat file. To save you from downloading every single combination (or at least every element) in JavaScript, were going to generate it on the fly at the server end, and fetch it for the client with jQuery. jQuery integrates perfectly with normal JavaScript, so youll find it an easy task to work it into your code.Server-side Code (PHP)To keep it simple, well use the basic code below to create our concept generator. Dont worry about how it works, just look at what it does: it outputs a randomized quote. Note that this code doesnt output XML it merely outputs raw text:Here, Ive used the Cache-Control header response because Internet Explorer has a habit of caching pages that have the same URL, even if the content between the pages differs. Obviously, that defeats the purpose of our script the production of a new quote on every load. We could have used jQuery to include a random number in the URL that would then be discarded, but its easier to address this caching issue on the server side than the client side.Client-side Code (HTML)Lets start creating the HTML for the front end, then work our Ajax into it. All we need on the page is a button that users can click to request another quote, and a div into which well put the quote once weve received it from the server. Well use jQuery to select this div and load the quote into it, and well reference the div by its id. If we wanted to, we could use jQuery to load the quote into multiple elements, with the help of a class, but an id is all we need for now. Lets make this the content of our body element:We can put the quote itself inside the div. Normally, wed have a lengthy onSubmit event for the button (the input with the id generate). Sometimes, wed have an onSubmit event handler that called a JavaScript function. But with jQuery, we dont even need to touch the HTML we can separate behavior (the event handler) from the structure (the page HTML) with ease.Client-side Code (jQuery)Its time to bring our back end together with our front end using jQuery. I mentioned earlier that we can select elements from the DOM with jQuery. First, we have to select the button and assign an onClick event handler to it. Within the code for this event, we can select the div and load the content of our script into it. Heres the syntax for the click event handler:$(element expression).click(function()/ Code goes here);As you probably already know, if we were to select this element in CSS, the # would identify that we were making our selection using the elements id attribute. You can use exactly the same syntax with jQuery. Therefore, to select the button with the id generate (which we gave it above), we can use the element expression #generate. Also, be aware that this syntax defines our event handler as an anonymous function within the event itself.Mark Wubbens JavaScript Terminology page offers a great explanation of anonymous functions, if youd like to know more.Were going to use one of jQuerys higher level Ajax functions, load(). Lets assume that our generator script is saved as script.php. Lets integrate it with our client side with the help of the load() function:$(#generate).click(function()$(#quote).load(script.php););Thats it: three lines of code, and we have fully functioning Ajax random quote generator! Well, almost.The problem with JavaScript is that code thats not within a function is executed as soon as the browser reaches it during rendering not once the page has finished rendering. As such, this code will try to attach to an element that has not yet loaded. Normally, wed use window.onload to deal with this issue. However, the limitation with that approach is that window.onload is called once everything has finished loading images and all. Were not interested in waiting for those images its just the DOM that we want access to.Fortunately, jQuery has $(document).ready(), which, as its name suggests, is executed when the DOM is ready to be manipulated.The Complete CodeHeres the complete code, including the $(document).ready wrapper and some basic HTML and CSS: Ajax with jQuery Example $(document).ready(function() $(#generate).click(function() $(#quote p).load(script.php); ); ); #wrapper width: 240px; height: 80px; margin: auto; padding: 10px; margin-top: 10px; border: 1px solid black; text-align: center; This code is also included in this downloadable zip file. Remember, this code assumes the jQuery library has been saved as jquery.js in the same folder as the PHP script and the HTML front end. Now that youre familiar with jQuery, lets move on to something more complicated: form elements and XML handling. This is true Ajax!中文译文简单的Ajax和jQuery的文章Ajax是改变web应用程序中,给他们一个反应是闻所未闻的超越桌面。但在所有的大肆宣传,没有太多的Ajax -(X)HTML、JavaScript和XML是没有什么新东西,并且在本教程中,我将向您展示如何简化过程添加Ajax应用程序甚至进一步借助jQuery,一个流行的JavaScript库。什么是ajax ?Ajax是改变web应用程序中,给他们一个反应是闻所未闻的超越桌面。但在所有的大肆宣传,没有太多的Ajax -(X)HTML、JavaScript和XML是没有什么新东西,并且在本教程中,我将向您展示如何简化过程添加Ajax应用程序甚至进一步借助jQuery,一个流行的JavaScript库。不幸的是,实际的方法深入教程进入世界上的Ajax是少之又少。添加到这个问题,XMLHttpRequest类使用了Ajax不是很容易,web开发人员开始使用。幸运的是,大量的JavaScript库提供了一种更简单的方法。今天,我将向您展示如何jQuery的这些库,允许你轻松地添加Ajax应用程序。什么是jquery?jQuery是另一个成熟的JavaScript库,提供了一些特性,别人不。诚然,它并不完全一样轻便的其他一些产品:jQuery排在19 kb,而哞。外汇只有3 kb。你可以阅读更多关于jQuery在世界杯上的JavaScript库的比较,其他一些JavaScript库,提供类似的功能。认识知识?为了完成本教程,您将需要一些基本的JavaScript知识。如果你知道任何c风格的语言,你会得到游戏中的JavaScript在没有时间。只是觉得花括号,函数声明,和可选的分号每一行的末尾(他们不会随意与jQuery,尽管)。如果你渴望开始使用JavaScript,看到这个优秀的、简洁的JavaScript教程为程序员。同时,因为我们正在谈论web应用程序,具备基本的HTML是必需的。Jquery 101?让我们浏览一下jQuery快速的介绍。能够使用它在你的页面,您首先需要下载该库。你可以在这里下载最新版本1.1.2在撰写本文的时候。jQuery的方法很简单:找到的东西,做的东西。我们从文档中选择元素(通过DOM)使用jQuery函数,别名$()。这个方便的函数的行为就像document . getelementbyid(),不过不是只支持IDs,它支持CSS选择器和一些XPath选择器,而没有返回一个元素,它可以返回一个数组的元素。好吧,或许一种更好的方式来描述$(),它就像document . getelementbyid()在类固醇。然后,我们使用函数来执行行动对我们的选择。例如,要添加文本“Hello World !“所有的div与类的foo,然后设置颜色为红色,我们会用下面的代码: $(div.foo).append(Hello World!).css(color,red);简单!通常,这个任务需要两行代码,就像这样:$(div.foo).append(Hello World!);$(div.foo).css(color,red);jQuery的链的方法允许我们写更紧凑的代码比其他JavaScript库。这些函数在jQuery中,不需要一个对象,因为它们独立工作,很多Ajax功能属于这个组。例如,post函数,我们将马上利用叫做打字$ . post(参数)。更多的jQuery函数、检查或在线文档。示例1-我们的第一个Ajax应用程序作为一个例子,我们要制定一个交互式概念生成器。基本上,这涉及到我们的选择两个术语随意地从一个列表,然后将它们组合起来创建一个短语。对于这个练习,我们将使用web 2.0术语(“混搭”、“大众分类法”、“媒体”等等),通常我们会拿这些术语从一个平面文件。为了不让你下载每个组合(或至少每个元素)在JavaScript中,我们将生成它飞在服务器端和客户端获取与jQuery。jQuery集成完全正常的JavaScript,因此你会发现它是一个简单的任务才能进入你的代码。服务器端的代码(php)为简单起见,我们将使用以下代码来创建我们的基本概念生成器。不要担心它是如何工作的,只是看看它所做的:它输出一个随机的报价。注意,这段代码并不输出XML -它仅仅输出原始文本:这里,我使用了ie浏览器cache - control头部响应。因为有一个习惯缓存的页面具有相同的URL,即使内容页面之间的不同。很明显,这一目的的脚本一个新的引用的生产在每个负载。我们可以使用jQuery来包括一个随机数,然后在URL被丢弃,但很容易解决这个问题在服务器端缓存比客户端。客户端代码(HTML)让我们开始创建的HTML前端,那么我们的Ajax工作它。所有我们需要的页面是一个按钮,用户可以点击请求另一个报价,一个div,我们要把这个报价,一旦我们收到从服务器。我们将使用jQuery选择此div和加载报价,我们将参考div的id。如果我们想,我们可以使用jQuery来加载引用到多个元素的帮助下,一个类,但是一个id,所有我们需要的只是现在。让我们把这个内容我们身体的元素: 我们可以把报价里面div。通常,我们会有一个漫长的onSubmit事件为按钮(输入id为“生成”)。有时,我们就有了一个onSubmit事件处理程序,称为一个JavaScript函数。但在jQuery中,我们甚至不需要触碰HTML我们可以单独的行为(事件处理程序)从结构(页
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 游路施工方案(3篇)
- 县级单位联谊活动策划方案(3篇)
- 商场活动美陈策划方案(3篇)
- 读书活动策划方案公司文案(3篇)
- 志愿引导员活动方案策划(3篇)
- 抚顺体育活动方案策划(3篇)
- 兴城教师考试题库及答案
- 上海写字考试题库及答案
- 管理岗位考试题库及答案
- 心里性格测试题目及答案
- 人体解剖实验管理制度
- 夏季安全生产试题及答案
- 二氧化硅包覆金纳米粒子核壳结构的构筑及负载染料后的性能与应用探索
- 配网防外破管理制度
- 2025至2030年中国饲料酶制剂行业市场需求分析及投资方向研究报告
- 不寐的中医辨证论治课件
- 7.4 一元一次不等式组 (课件)华东师大版数学七年级下册
- 天府新区招商推介报告
- 体育旅游市场结构分析及创新产品开发路径研究
- 初中体育与健康排球运动作业设计
- 高空作业安全技术交底完整
评论
0/150
提交评论