13

Click here to load reader

jQuery

Embed Size (px)

Citation preview

Page 1: jQuery

jQuery TutorialjQuery is a JavaScript Library.

jQuery greatly simplifies JavaScript programming.

jQuery is easy to learn.

jQuery support Cross Browser Compatability.

What is jQuery?jQuery is a lightweight, "write less, do more", JavaScript library.

The purpose of jQuery is to make it much easier to use JavaScript on your website.

jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.

The jQuery library contains the following features:

HTML/DOM manipulation CSS manipulation

HTML event methods

Effects and animations

AJAX

Utilities

Tip: In addition, jQuery has plugins for almost any task out there.

Why jQuery?There are lots of other JavaScript frameworks out there, but jQuery seems to be the most popular, and also the most extendable.

Many of the biggest companies on the Web use jQuery, such as:

Google Microsoft

Page 2: jQuery

IBM

Netflix

Will jQuery work in all browsers?

The jQuery team knows all about cross-browser issues, and they have written this knowledge into the jQuery library. jQuery will run exactly the same in all major browsers, including Internet Explorer 6!

jQuery InstallAdding jQuery to Your Web PagesThere are several ways to start using jQuery on your web site. You can:

Download the jQuery library from jQuery.com Include jQuery from a CDN, like Google

Downloading jQueryThere are two versions of jQuery available for downloading:

Production version - this is for your live website because it has been minified and compressed

Development version - this is for testing and development (uncompressed and readable code)

Both versions can be downloaded from jQuery.com.

The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag (notice that the <script> tag should be inside the <head> section):

<head><script src="jquery-1.10.2.min.js"></script></head>

Tip: Place the downloaded file in the same directory as the pages where you wish to use it.

Do you wonder why we do not have type="text/javascript" inside the <script> tag?

Page 3: jQuery

This is not required in HTML5. JavaScript is the default scripting language in HTML5 and in all modern browsers!

Alternatives to DownloadingIf you don't want to download and host jQuery yourself, you can include it from a CDN (Content Delivery Network).

Both Google and Microsoft host jQuery.

To use jQuery from Google or Microsoft, use one of the following:

Google CDN:<head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script></head>

Get the latest available version with Google CDN:

If you look at the Google URL above - the version of jQuery is specified in the URL (1.10.2). If you would like to use the latest version of jQuery, you can either remove a number from the end of the version string (for example 1.10), then Google will return the latest version available in the 1.10 series (1.10.0, 1.10.1, etc.), or you can take it up to the whole number (1), and Google will return the latest version available in the 1 series (from 1.1.0 to 1.10.2).

Microsoft CDN:

<head><script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js"></script></head>

One big advantage of using the hosted jQuery from Google or Microsoft:

Many users already have downloaded jQuery from Google or Microsoft when visiting another site. As a result, it will be loaded from cache when they visit your site, which leads to faster loading time. Also, most CDN's will make sure that once a user requests a file from it, it will be served from the server closest to them, which also leads to faster loading time.

Page 4: jQuery

jQuery SyntaxThe jQuery syntax is tailor made for selecting HTML elements and performing some action on the element(s).

Basic syntax is: $(selector).action()

A $ sign to define/access jQuery A (selector) to "query (or find)" HTML elements

A jQuery action() to be performed on the element(s)

Are you familiar with CSS selectors?

jQuery uses CSS syntax to select elements. You will learn more about the selector syntax in the next chapter of this tutorial.

The Document Ready EventYou might have noticed that all jQuery methods in our examples, are inside a document ready event:

$(document).ready(function(){

   // jQuery methods go here...

});

This is to prevent any jQuery code from running before the document is finished loading (is ready).

It is good practice to wait for the document to be fully loaded and ready before working with it. This also allows you to have your JavaScript code before the body of your document, in the head section.

Here are some examples of actions that can fail if methods are run before the document is fully loaded:

Trying to hide an element that is not created yet Trying to get the size of an image that is not loaded yet

Tip: The jQuery team has also created an even shorter method for the document ready event:

$(function(){

   // jQuery methods go here...

});

Use the syntax you prefer. We think that the document ready event is easier to understand when reading the code.

Page 5: jQuery

jQuery SelectorsjQuery selectors allow you to select and manipulate HTML element(s).

jQuery selectors are used to "find" (or select) HTML elements based on their id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.

All selectors in jQuery start with the dollar sign and parentheses: $().

The element SelectorThe jQuery element selector selects elements based on the element name.

You can select all <p> elements on a page like this:

$("p")

Example

When a user clicks on a button, all <p> elements will be hidden:

Example$(document).ready(function(){  $("button").click(function(){    $("p").hide();  });});

The #id SelectorThe jQuery #id selector uses the id attribute of an HTML tag to find the specific element.

An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

To find an element with a specific id, write a hash character, followed by the id of the element:

$("#test")

Example

When a user clicks on a button, the element with id="test" will be hidden:

Page 6: jQuery

Example$(document).ready(function(){  $("button").click(function(){    $("#test").hide();  });});

The .class SelectorThe jQuery class selector finds elements with a specific class.

To find elements with a specific class, write a period character, followed by the name of the class:

$(".test")

Example

When a user clicks on a button, the elements with class="test" will be hidden:

Example$(document).ready(function(){  $("button").click(function(){    $(".test").hide();  });});

jQuery Selectors

Selector Example Selects

* $("*") All elements

#id $("#lastname") The element with id="lastname"

.class $(".intro") All elements with class="intro"

.class,.class $(".intro,.demo") All elements with the class "intro" or "demo"

element $("p") All <p> elements

el1,el2,el3 $("h1,div,p") All <h1>, <div> and <p> elements

Page 7: jQuery

     

:first $("p:first") The first <p> element

:last $("p:last") The last <p> element

:even $("tr:even") All even <tr> elements

:odd $("tr:odd") All odd <tr> elements

     

:first-child $("p:first-child") All <p> elements that are the first child of their parent

:first-of-type $("p:first-of-type") All <p> elements that are the first <p> element of their parent

:last-child $("p:last-child") All <p> elements that are the last child of their parent

:last-of-type $("p:last-of-type") All <p> elements that are the last <p> element of their parent

:nth-child(n) $("p:nth-child(2)") All <p> elements that are the 2nd child of their parent

:nth-last-child(n) $("p:nth-last-child(2)") All <p> elements that are the 2nd child of their parent, counting from the last child

:nth-of-type(n) $("p:nth-of-type(2)") All <p> elements that are the 2nd <p> element of their parent

:nth-last-of-type(n) $("p:nth-last-of-type(2)") All <p> elements that are the 2nd <p> element of their parent, counting from the last child

:only-child $("p:only-child") All <p> elements that are the only child of their parent

Page 8: jQuery

:only-of-type $("p:only-of-type") All <p> elements that are the only child, of its type, of their parent

     

parent > child $("div > p") All <p> elements that are a direct child of a <div> element

parent descendant $("div p") All <p> elements that are descendants of a <div> element

element + next $("div + p") The <p> element that are next to each <div> elements

element ~ siblings $("div ~ p") All <p> elements that are siblings of a <div> element

     

:eq(index) $("ul li:eq(3)") The fourth element in a list (index starts at 0)

:gt(no) $("ul li:gt(3)") List elements with an index greater than 3

:lt(no) $("ul li:lt(3)") List elements with an index less than 3

:not(selector) $("input:not(:empty)") All input elements that are not empty

     

:header $(":header") All header elements <h1>, <h2> ...

:animated $(":animated") All animated elements

:focus $(":focus") The element that currently has focus

:contains(text) $(":contains('Hello')") All elements which contains the text "Hello"

:has(selector) $("div:has(p)") All <div> elements that have a <p> element

Page 9: jQuery

:empty $(":empty") All elements that are empty

:parent $(":parent") All elements that are a parent of another element

:hidden $("p:hidden") All hidden <p> elements

:visible $("table:visible") All visible tables

:root $(":root") The document's root element

:lang(language) $("p:lang(de)") All <p> elements with a lang attribute value starting with "de"

     

[attribute] $("[href]") All elements with a href attribute

[attribute=value] $("[href='default.htm']") All elements with a href attribute value equal to "default.htm"

[attribute!=value] $("[href!='default.htm']") All elements with a href attribute value not equal to "default.htm"

[attribute$=value] $("[href$='.jpg']") All elements with a href attribute value ending with ".jpg"

[attribute|=value] $("[title|='Tomorrow']") All elements with a title attribute value equal to 'Tomorrow', or starting with 'Tomorrow' followed by a hyphen

[attribute^=value] $("[title^='Tom']") All elements with a title attribute value starting with "Tom"

[attribute~=value] $("[title~='hello']") All elements with a title attribute value containing the specific word "hello"

[attribute*=value] $("[title*='hello']") All elements with a title attribute value containing the word "hello"

Page 10: jQuery

     

:input $(":input") All input elements

:text $(":text") All input elements with type="text"

:password $(":password") All input elements with type="password"

:radio $(":radio") All input elements with type="radio"

:checkbox $(":checkbox") All input elements with type="checkbox"

:submit $(":submit") All input elements with type="submit"

:reset $(":reset") All input elements with type="reset"

:button $(":button") All input elements with type="button"

:image $(":image") All input elements with type="image"

:file $(":file") All input elements with type="file"

:enabled $(":enabled") All enabled input elements

:disabled $(":disabled") All disabled input elements

:selected $(":selected") All selected input elements

:checked $(":checked") All checked input elements

jQuery Event MethodsWhat are Events?All the different visitor's actions that a web page can respond to are called events.

An event represents the precise moment when something happens.

Page 11: jQuery

Examples:

moving a mouse over an element selecting a radio button

clicking on an element

The term "fires" is often used with events. Example: "The keypress event fires the moment you press a key".

Here are some common DOM events:

Mouse Events Keyboard Events Form Events Document/Window Events

click keypress submit load

dblclick keydown change resize

mouseenter keyup focus scroll

mouseleave   blur unload

jQuery Syntax For Event MethodsIn jQuery, most DOM events have an equivalent jQuery method.

To assign a click event to all paragraphs on a page, you can do this:

$("p").click();

The next step is to define what should happen when the event fires. You must pass a function to the event:

$("p").click(function(){  // action goes here!!});