`
seebysee
  • 浏览: 10788 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

Simplify Ajax development with jQuery 使用Jquery简化Ajax开发(I)

阅读更多
Simplify Ajax development with jQuery
使用Jquery简化Ajax开发
Discover how easy Ajax and DOM scripting can be
原来DOMAjax是如此简单
 
Level: Intermediate
文章等级:中级
Jesse Skinner, Web Developer, Freelance
Jesse SkinnerWeb 开发,自由撰稿人
10 Apr 2007
jQuery is a JavaScript library that helps simplify your JavaScript™ and Asynchronous JavaScript + XML (Ajax) programming. Unlike similar JavaScript libraries, jQuery has a unique philosophy that allows you to express common complex code succinctly. Learn about the jQuery philosophy, discover its features and functions, perform some common Ajax tasks, and find out how to extend jQuery with plug-ins.
jQuery 是一种可以简化javascript脚本和Ajax开发的一组库函数。与其他和javascript相似的库函数不同,jQuery观点是让你使用简洁的开发出通常很复杂的代码。学习jQuery的观点,探索它的新特性和新功能,实现一些常见的Ajax功能,并且会学会使用Jquery的一些扩展插件。
什么是jQuery
Created by John Resig in early 2006, jQuery is a great library for anyone who works with JavaScript code. Whether you're new to the JavaScript language and want a library to address some of the complexities of Document Object Model (DOM) scripting and Ajax or you're an advanced JavaScript guru and tired of the boring repetition involved with DOM scripting and Ajax, jQuery will suit you well.
jQuery helps you keep your code simple and succinct. You no longer have to write a bunch of repetitious loops and DOM scripting library calls. With jQuery, you can get right to the point and express yourself in the fewest possible characters.
The jQuery philosophy is certainly unique: It's designed to keep things simple and reusable. When you understand and feel comfortable with the philosophy, you'll start to see just how much jQuery can improve the way you program.
2006John Resig 创造jQuery,这是一个对于任何一个使用Javascript工作的人都是一个相当不错的库。不论你是刚刚学会Javascript并且需要一个库来做一些复杂的DOM操作脚本,实现一些Ajax;还是一位已经厌倦了包含大量重复代码的资深的Javascript大师,jQuery将是一个很好的选择。
 
 
一些简化的例子
Here's a simple example of the impact jQuery can have on your code. To do something really simple and common, such as attach a click event to every link in an area of a page, you'd use plain JavaScript code and DOM scripting, as shown in Listing 1.
这有一些可能会出现在你的项目里的一些简洁的jQuery代码。展现了一些简单而又常见的功能,例如使页面上的某段区域的每一个链接都附加上一个单击事件,你可能会用到像Listing1那样的JavascriptDOM脚本。
 
        
var external_links = document.getElementById('external_links');
var links = external_links.getElementsByTagName('a');
for (var i=0;i < links.length;i++) {
    var link = links.item(i);
    link.onclick = function() {
        return confirm('You are going to visit: ' + this.href);
    };
}
 
Listing 2 shows the same functionality achieved using jQuery.
        
$('#external_links a').click(function() {
    return confirm('You are going to visit: ' + this.href);
});
 
Amazing, isn't it? With jQuery, you get right to the point, and express only what you want the code to do without all the hassle. No need to loop over the elements; the click() function takes care of that. Also no need to make multiple DOM scripting calls: All that you need is a short string to define the elements to work on.
是不是很令人惊讶?使用jQuery,直切正题,毫不犹豫的写出你所需要的代码。不需要遍历所有的元素。Click()方法负责这项任务。当然也不需要重复的DOM脚本调用;所有你需要作的就是一小段代码去定义元素开始如何工作;
It can be a bit tricky to understand how this code even gets the job done. First, you have the $() function -- the most powerful function in jQuery. Mostly, you use this function to select elements from the document. In this example, the function is passed a string containing some Cascading Style Sheets (CSS) syntax, and jQuery finds the elements as efficiently as possible.
解释这些代码使如何工作的可能会有一些复杂。首先,你使用了$()方法――jQuery中最强大的方法。大多数情况下,你使用它来从文档对象中选择元素对象,这个方法传送一个包含CSS变量的字符串,jQuery会以最有效率的方法找到它。
If you know the basics of CSS selectors, this syntax should look familiar. In Listing 2, #external_links looks for the element with an id of external_links. A space followed by a tells jQuery to look for all the <a> elements inside the external_links element. That's a mouthful to say in English -- and even in DOM scripting -- but in CSS, it couldn't be simpler.
 
如果你比较熟悉CSS的选择器,在Listing2中的这个变量可能会看起开很熟悉,
#external_links寻找id#external_links的所有元素。一个a后面的空格告诉jQuery去寻找所有在#external_links内的<a>标签的元素对象。可能在DOM之中不会是最简洁的写法,但是对于CSS,这已经是最简单的写法了。
The $() function returns a jQuery object containing all the elements that match the CSS selector. A jQuery object is something like an array but comes with a ton of special jQuery functions. For example, you can assign a click handler function to each element in the jQuery object by calling the click function.
You can also pass an element or an array of elements to the $() function, and it will wrap a jQuery object around the elements. You might want to use this functionality to employ jQuery functions on things like the window object. For example, you typically assign the function to the load event like this:
$()方法返回所有包含在所符合CSS选择器的内的jQuery对象。一个jQuery对象就像是一个数组,但是它包含了许多特殊的函数。例如,你可以通过调用click方法为每一个元素对象分配一个点击函数的句柄。
 
window.onload = function() {
    // do this stuff when the page is done loading
};
 
Using jQuery, you write the same code like this:
$(window).load(function() {
    // run this when the whole page has been downloaded
});
 
As you probably already know, waiting for the window to load is painfully slow, because the whole page must finish loading, including all the images on the page. Sometimes, you want the images to finish loading first, but most of the time, you just need the Hypertext Markup Language (HTML) to be there. jQuery solves this problem by creating a special ready event on the document, used like this:
你可能知道,等待一个窗口的缓慢是一件非常痛苦的事情,因为这个页面必须完全载入,包括所有页面上的图片。有些时候,你可能会需要一些图片首先被载入,但是大多数的情况下,你仅仅需要的是HTML语句。jQuery通过ready时间解决了这个问题,甚至是整个文档对象。
$(document).ready(function() {
    // do this stuff when the HTML is all ready
});
 
This code creates a jQuery object around the document element, then sets up a function to call the instance when the HTML DOM document is ready. You can call this function as many times as necessary. And, in true jQuery style, a shortcut calls this function. Simply pass a function to the $() function:
这段代码围绕document元素对象建立了一个jQuery对象,然后当页面DOM元素被载入的时候使用一个方法调用它的实例。你可以按照需要的次数条用这个方法。而且,实际情况下,可以使用$()方法
简化这一切
$(function() {
    // run this when the HTML is done downloading
});
 
So far, I've shown you three different ways to use the $() function. With a fourth way, you can create an element using a string. The result is a jQuery object containing that element. Listing 3 shows an example that adds a paragraph to the page.
到现在,我已经从三个不同的方面向你介绍$()方法的使用方法。在第四种方法里,你可以使用字符串建立元素。返回的结果是一个包含哪个元素对象的jQuery对象。
        
$('<p></p>')
    .html('Hey World!')
    .css('background', 'yellow')
    .appendTo("body");
 
$('#message').css('background', 'yellow').html('Hello!').show();
 
通过前面几个例子,你可能注意到jQuery的另一个强大的特性是method chaining(函数链)。每当你在jQuery对象中调用一个方法的时候,这个方法同样会返回一个jQuery对象。这就意味着当你需要对一个jQuery对象使用一连串方法的时候,你可以这样写:
分享到:
评论

相关推荐

    JQuery In Action.PDF

    - **Utility Functions:** JQuery includes a set of utility functions that simplify common tasks such as manipulating CSS styles, adding classes, or applying animations. - **Example:** `.addClass(...

    easy-ajax:基于 jQuery 的 Ajax 调用封装,简化常用场景下的 Ajax 调用

    易阿贾克斯 Based jQuery to build easy ajax api, to simplify ajax request for:1. Dialog2. Form, and submitting3. HTTP asynchronous loading4. others先决条件 1. jQuery2. jQuery Dialog Model安装 1. Setup ...

    Unity使用Mesh Simplify 减面(网格简化)

    此插件用于简化网格 使用方法: 只需要添加组件"Mesh Simplify"组件到需要简化网格的对象上,点击Compute mesh即可, 如果操作报错的话,开启模型的Read/write Enabled就好了 操作简单,方便使用

    [JavaScript] JavaScript 从入门到精通 (英文版)

    Speed up and simplify app development with jQuery Quickly retrieve data from a server using AJAX requests Adapt your app for mobile devices with jQuery Mobile Build Windows 8 apps using HTML, CSS, and...

    《 jQuery实战.pdf》高清下载

    9. 简化脚本编写(Simplify Scripting):jQuery通过消除跨浏览器兼容问题,提供简洁的API,使得编写和维护JavaScript代码变得更加简单。 10. 社区支持(Community Support):由于其广泛的用户群和活跃的社区,...

    jquery Simplify your JavaScript

    Matthew Dawes Pacific Northwest National Laboratory Interlab 2007

    Java.EE.7.Development.with.NetBeans.8

    NetBeans has several features that greatly simplify Java EE development, but with many features and great flexibility, Java developers can become overwhelmed by the options available in NetBeans. ...

    MeshSimplify.rar

    总之,MeshSimplify.rar中的模型减面插件是Unity开发人员的理想工具,它提供了高效且易于使用的模型简化解决方案,帮助开发者在性能和视觉效果之间找到最佳平衡。通过深入理解和利用这个工具,开发者可以提升他们的...

    simplify-js, 高性能JavaScript多段线简化库.zip

    simplify-js, 高性能JavaScript多段线简化库 Simplify.js 是由 Vladimir Agafonkin简化的高性能JavaScript多线线简化库,从 Leaflet中提取。使用文档签出演示: http://mourner.github.io/simplify-js/端

    simplify-geometry:使用 Ramer-Douglas-Peucker 算法简化几何

    简化几何 使用简化几何。 如果您需要简化几何图形,例如减少多边形中的点数,这将对您有所帮助。 您可以在使用它。节点例子 var simplify = require ( 'simplify-geometry' ) ;var linestring = [ [ 0 , 0 ] , [ 2.5...

    turf-simplify:简化地理形状

    草皮简化 简化地理形状turf.simplify(feature, tolerance, highQuality) 接受LineString或Polygon并返回简化版本。 内部使用进行简化。参数范围类型描述feature 功能。&lt;LineString&gt; tolerance 数字简化公差...

    Professional.ASP.NET.2.0.AJAX

    This detailed guide to the Microsoft AJAX Library shows you how to bring together ASP.NET 2.0 with an object-oriented approach to JavaScript development using this new framework from Microsoft....

    Simplify-Testing-with-React-Testing-Library:Packt发布的使用React Testing Library简化测试

    使用React Testing库简化测试 这是Packt发布的“ 的代码库。 使用RTL创建可保持不变的测试 这本书是关于什么的? React Testing Library(RTL)是一个轻巧且易于使用的工具,用于测试组件的文档对象模型(DOM)...

    藏经阁-Simplify Machine Learning With Flink TableAPI.pdf

    Flink TableAPI 是 Apache Flink 的一部分,是一个高级别的分析 API,旨在简化机器学习的开发过程。Table API 提供了一个统一的接口来处理批处理和流处理数据,能够满足机器学习算法对 API 的核心需求。 Table API ...

    MeshSimplify:网格简化的实用程序

    MeshSimplify 是一个多边形网格简化程序。 它可用于创建具有任意数量面的输入网格的简化变体。 此外,该程序能够生成渐进式网格表示,作为简化过程的一部分,可用于 3d 对象的平滑多分辨率渲染。 使用MeshSimplify ...

    simplify-1.2.1.jar

    Simplify工具 反混淆 最新版本simplify.jar,参考https://github.com/CalebFenton/simplify 编译而成

    Mesh Simplify插件 v1.10.rar

    Mesh Simplify的核心功能在于模型减面,即通过算法将三维模型的几何信息进行简化,降低模型的多边形数量,从而减少计算量,提高运行效率。这个过程并不会显著影响模型的整体外观,保持了视觉上的逼真度。对于游戏...

Global site tag (gtag.js) - Google Analytics