jQuery

From Wikipedia, the free encyclopedia

Jump to: navigation, search
jQuery
Image:Jquerylogo.png
Developed by jQuery Team
Latest release 1.3.2 / 2009-02-20; 46 days ago
Written in JavaScript
Type Web application framework
License Dual license:
GPL and MIT
Website http://jquery.com/

jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML. It was released January 2006 at BarCamp NYC by John Resig.

Dual licensed under the MIT License and the GNU General Public License, jQuery is free, open source software.

Both Microsoft and Nokia have announced plans to bundle jQuery[1] on their platforms, Microsoft adopting it initially within Visual Studio[2] and use within Microsoft's ASP.NET AJAX framework and ASP.NET MVC Framework whilst Nokia will integrate it into their Web Run-Time platform.

Contents

[edit] Philosophy

Just as CSS separates "display" characteristics from the HTML structure, jQuery separates the "behavior" characteristics from the HTML structure. For example, instead of directly specifying the on-click event handler in the specification of a button element, a jQuery driven page would first identify the button element, and then modify its on-click event handler. This separation of behavior from structure is also referred to as the principle of Unobtrusive JavaScript.

[edit] Features

jQuery contains the following features:

  • DOM element selections
  • DOM traversal and modification, (including support for CSS 1-3 and basic XPath)
  • Events
  • CSS manipulation
  • Effects and animations
  • Ajax
  • Extensibility
  • Utilities - such as browser version and the each function.
  • JavaScript Plugins

[edit] The $ function

One of the critical concepts in any jQuery code is the so called '$' function. '$' is actually an 'alias' for the 'jQuery' namespace.

Example 1: jQuery provides a function for trimming strings. This function can be used as:

str = "    foo     ";
jQuery.trim(str); // returns "foo"

Or, it can also be used as:

str = "    foo     ";
$.trim(str);

These are equivalent. Usage of '$' instead of 'jQuery' is an ad-hoc convention, and is considered easier.

Example 2: To select all the paragraphs that have the class 'foo' and add another class called 'bar' to all of them:

$("p.foo").addClass("bar");

Example 3: To execute a function 'myfunc' immediately after the page is loaded (called the ready handler in jQuery lingo):

$(myfunc);

This is typically used in a context like this:

$(function()
{
  // Stripe all the tables in the document using the oddStripe and evenStripe CSS classes.
  $('tr:nth-child(odd)').addClass("oddStripe");
  $('tr:nth-child(even)').addClass("evenStripe");
});

[edit] Use

jQuery exists as a single JavaScript file, containing all the common DOM, Event, Effects, and Ajax functions. It can be included within any web page by using the following markup:

<script type="text/javascript" src="/path/to/jQuery.js"></script>

jQuery has two styles of interaction:

  • via the $ function, which is a factory method for the jQuery object. These functions, often called commands, are chainable; they each return the jQuery object
  • via $.-prefixed functions. These are utility functions which do not work on the jQuery object per se.

A typical workflow for manipulation of multiple DOM nodes begins with $ function being called with a CSS selector string, with results in the jQuery object referencing zero or more elements in the HTML page. This node set can be manipulated by applying instance methods to the jQuery object, or the nodes themselves can be manipulated. For example:

$("div.test").add("p.quote").addClass("blue").slideDown("slow");

…finds the union of all div tags with class attribute test and all p tags with class attribute quote, adds the class attribute blue to each matched element, and then slides them down with an animation. The $ and add functions affect the matched set, while the addClass and slideDown affect the referenced nodes.

The methods prefixed with $. are convenience methods or affect global properties and behaviour. For example, the following is an example of the map function called each in jQuery:

$.each([1,2,3], function() {
  document.write(this + 1);
});

... writes 234 to the document.

It is possible to perform Ajax routines using the $.ajax and associated methods to load and manipulate remote data.

$.ajax({
  type: "POST",
  url: "some.php",
  data: "name=John&location=Boston",
  success: function(msg){
    alert( "Data Saved: " + msg );
  }
});

... will request some.php with parameters name=John and location=Boston and when the request is finished successfully, the response will be alerted.

[edit] Release History

Release Date Version Number Additional Notes
February 20, 2009 1.3.2
January 21, 2009 1.3.1
January 14, 2009 1.3
May 24, 2008 1.2.6
May 21, 2008 1.2.5 Fix for bad build of 1.2.4
May 19, 2008 1.2.4
February 8, 2008 1.2.3
January 15, 2008 1.2.2
September 16, 2007 1.2.1
September 10, 2007 1.2
August 24, 2007 1.1.4
July 5, 2007 1.1.3.1
July 1, 2007 1.1.3
May 20, 2007 1.1.3a Alpha Release
February 27, 2007 1.1.2
January 22, 2007 1.1.1
January 14, 2007 1.1
January 8, 2007 1.1a Alpha Release
December 12, 2006 1.0.4 Last 1.0 bug fix
October 27, 2006 1.0.3
October 9, 2006 1.0.2
August 31, 2006 1.0.1
August 26, 2006 1.0 First Stable Release
June 30, 2006 1.0a Alpha Release

[edit] See also

[edit] References

  1. ^ Resig, John (2008-09-28). "jQuery, Microsoft, and Nokia". jQuery Blog. jQuery. http://jquery.com/blog/2008/09/28/jquery-microsoft-nokia/. Retrieved on 2009-01-29. 
  2. ^ Scott, Guthrie (2008-09-28). "jQuery and Microsoft". ScottGu's Blog. http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-and-microsoft.aspx. Retrieved on 2009-01-29. 

[edit] Further reading

[edit] External links

Personal tools