jquery learning

67
Learning jQuery

Upload: uzair-ali

Post on 15-Feb-2017

72 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: jQuery Learning

Learning jQuery

Page 2: jQuery Learning

Raise your hand if …• … you write cascading style sheets (css)• … you write ASP.net software?• … you write javascript?• … you write jQuery?• … you enjoy writing javascript?

Page 3: jQuery Learning

What’s the problem with JavaScript?

Page 4: jQuery Learning

JavaScript was a initially introduced in Netscape 2.0B3

in Dec 1995,

a.k.a. Mocha, LiveScript, Jscript, however, it’s official name is

ECMAScript

Page 5: jQuery Learning

JavaScript is a weakly typed, classless, prototype based OO language It is a browser DOM.

Page 6: jQuery Learning

The world’s most misunderstood

programming language.

(Douglas Crockford)

Page 7: jQuery Learning

Browser DOM really sucks, and this is where jQuery comes to

rescue.

Page 8: jQuery Learning

This session is about improving your Quality

of Life !

Page 9: jQuery Learning

Introduction to jQuery

Page 10: jQuery Learning

Just a few of jQuery's Benefits

• Less Line of Code.• Improves developer efficiency• Excellent documentation(Open Source)• Excellent presentation view• DOM navigation (css-like syntax)• Builder model (chain method calls)• Extensible and there are tons of libraries• Handles most browser differences

Page 11: jQuery Learning

Scott Guthrie’s BlogScott Guthrie is a Corporate Vice President in the Microsoft Developer Division. He runs the development teams that build

products/technologies. CLR and the core .NET Base Class Libraries ASP.NET  SilverlightWPFIIS 7.0 Visual Studio Tools for ASP.NET, WPF, Silverlight and MobilejQuery and Microsoft (Google ScottGu jQuery)

Page 12: jQuery Learning

jQuery and MicrosoftScott Guthrie announced “… that Microsoft will be shipping jQuery with Visual Studio going forward.  We will distribute the jQuery JavaScript library as-is, and will not be forking or changing the source from the main jQuery branch.  The files will continue to use and ship under the existing jQuery MIT license.

We will also distribute intellisense-annotated versions that provide great Visual Studio intellisense and help-integration at design-time”

Page 13: jQuery Learning

What is jQuery?

Page 14: jQuery Learning

jQuery is a lightweight, open-source JavaScript library that simplifies interaction between HTML and JavaScript and CSS

Page 15: jQuery Learning

It was and still being developed

by John Resig from Mozilla and was first

announced in January 2006

Page 16: jQuery Learning

jQuery Core Concepts

Page 17: jQuery Learning

DOM Scripting

Page 18: jQuery Learning

DOM

custom.js

Behavior Content

index.html

Presentation

style.css

Page 19: jQuery Learning

DOM Scripting

Page 20: jQuery Learning

DOM Scripting

•By Tag Name (getElementByTagName(“p”))

•By Class Name (getElementByclass(“p”))

•By ID Name (getElementByID(“p”))

Page 21: jQuery Learning

Unobtrusive

jquery.js

custom.js

Behavior Content

index.html

Presentation

style.css

Page 22: jQuery Learning

What we Need– jQuery Libraries– First Introduce 2006– 1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,

1.8.0,1.9.0.1.9.1,2.0.0 (http://jquery.com/)– Types:– Jquery.1.7. js(Size 271kb and response time 6ms)– Jquery.1.7.min.js (Size 90.4kb and response

time 2ms)

Page 23: jQuery Learning

What we Need– jQuery Libraries– Types Of jQuery:– jQuery Programming (jquery.js)– jQuery UI (jquery-ui.js) if use ui css then

(jquery-ui.css)– jQuery Mobile (jquery.mobile-1.3.1.js)

Page 24: jQuery Learning

Dev Tools

Page 25: jQuery Learning

Dev Tools• up to five browsers to test against

– Firefox (2.0+)– Chrome (8+)– Internet Explorer (6+)– Safari(3+)– Opera (10.8+)

• developer tools can make your life much easier

Page 26: jQuery Learning

$

Page 27: jQuery Learning

$( )

Page 28: jQuery Learning

$('div')

Page 29: jQuery Learning

$('#id')

Page 30: jQuery Learning

Manipulate existing DOM elements

$(window).width()

The Magic $() function

Page 31: jQuery Learning

var x,y;if (self.innerHeight) { // all except Explorer x = self.innerWidth; y = self.innerHeight;}else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode x = document.documentElement.clientWidth; y = document.documentElement.clientHeight;}else if (document.body) { // other Explorers x = document.body.clientWidth; y = document.body.clientHeight;}

DOM Scripting

Page 32: jQuery Learning

var x = $(window).width();var y = $(window).height();

jQuery

Page 33: jQuery Learning

Selects document elements(more in a moment…)

$(“div”).hide();

$(“div”, $(“p”)).hide();

The Magic $() function

Page 34: jQuery Learning

$(document).ready(function(){…});

Fired when the document is ready for programming.

Better use the full syntax:

$(function(){…});

The Magic $() function

Page 35: jQuery Learning

jQuery(“div”);

The full name of $() function is

Page 36: jQuery Learning

$(“div”).hide()

$(“:button”).click()

jQuery’s programming philosophy is:

GET >> ACT

Page 37: jQuery Learning

$(document).ready(function(){function(“#btn”).click});

Almost every function returns jQuery, which provides a fluent

programming interface and chainability:

Page 38: jQuery Learning

Get > Act

Chainability

The $() function

Three Major Concepts of jQuery

Page 39: jQuery Learning

jQuery Selectors

Page 40: jQuery Learning

$(“*”) // find everything

All Selector

Selectors return a pseudo-array of jQuery elements

Page 41: jQuery Learning

$(“div”)

// <div>Hello jQuery</div>

Basic Selectors

$(“#usr”)

// <span id=“usr”>John</span>

$(“.menu”)

// <ul class=“menu”>Home</ul>

By Tag:

By ID:

By Class:

Page 42: jQuery Learning

$(“div.main”) // tag and class

$(“table#data”) // tag and id

More Precise Selectors

Page 43: jQuery Learning

// find by id + by class

$(“#content, .menu”)

// multiple combination

$(“h1, h2, h3, div.content”)

Combination of Selectors

Page 44: jQuery Learning

$(“table td”) // descendants

$(“tr > td”) // children

$(“tr:even”) // even

$(“tr:odd”) // odd

Hierarchy Selectors

Page 45: jQuery Learning

$(“tr:first”) // first element

$(“tr:last”) // last element

$(“tr:lt(2)”) // index less than

$(“tr:gt(2)”) // index gr. than

$(“tr:eq(2)”) // index equals

Selection Index Filters

Page 46: jQuery Learning

$(“div: disabled”) // if disable

$(“div:enable”) // if enable

Disability Filters

Page 47: jQuery Learning

$(“div:visible”) // if visible

$(“div:hidden”) // if not

Visibility Filters

Page 48: jQuery Learning

$(“input:checkbox”) // checkboxes

$(“input:radio”) //radio buttons

$(“:button”) // buttons

$(“:text”) // text inputs

Forms Selectors

Page 49: jQuery Learning

$(“input:checked”) // checked

$(“input:selected”) // selected

$(“input:enabled”) // enabled

$(“input:disabled”) // disabled

Forms Filters

Page 50: jQuery Learning

SELECTORS DEMO

Page 51: jQuery Learning

HTML Manipulation

Page 52: jQuery Learning

$(“p”).html(“<div>Hello $!</div>”);

Getting and Setting Inner Content

Page 53: jQuery Learning

// select > append to the end$(“h1”).append(“<li>Hello $!</li>”);

// select > append to the beginning$(“ul”).prepend(“<li>Hello $!</li>”);

Inserting new Elements

Page 54: jQuery Learning

// get style$(“div”).css(“background-color”);

CSS Manipulations

// set style $(“div”).css(“float”, “left”);

// set multiple style properties$(“div”).css({“color”:”blue”,

“padding”: “1em” “margin-right”: “0”, marginLeft: “10px”});

Page 55: jQuery Learning

// from the document$(“div”).offset().top;

// from the parent element$(“div”).position().left;

// scrolling position$(window).scrollTop();

Positioning

Page 56: jQuery Learning

Events

Page 57: jQuery Learning

$(document).ready(function(){//…

});

When the DOM is ready…

• Fires when the document is ready for programming.

• Uses advanced listeners for detecting.• window.onload() is a fallback.

Page 58: jQuery Learning

EVENTS DEMO

Page 59: jQuery Learning

Effects

Page 60: jQuery Learning

// just show$(“div”).show();

// reveal slowly, slow=600ms$(“div”).show(“slow”);

// hide fast, fast=200ms$(“div”).hide(“fast”);

// hide or show in 100ms $(“div”).toggle(100);

Showing or Hiding Element

Page 61: jQuery Learning

$(“div”).slideUp();

$(“div”).slideDown(“fast”);

$(“div”).slideToggle(1000);

Sliding Elements

Page 62: jQuery Learning

$(“div”).fadeIn(“fast”);

$(“div”).fadeOut(“normal”);

// fade to a custom opacity$(“div”).fadeTo (“fast”, 0.5);

Fading Elements

Fading === changing opacity

Page 63: jQuery Learning

// .animate(options, duration)$(“div”).animate({

width: “90%”,opacity: 0.5,borderWidth: “5px”

}, 1000);

Custom Animation

Page 64: jQuery Learning

AJAX with jQuery

Page 65: jQuery Learning

$.get("demo_test.asp",function(data,status){alert("Data: " + data + "\nStatus: " + status);});

$.post(“test.aspx”, {id:1},function(data)

{alert(data);});

Sending GET/POST requests

Page 66: jQuery Learning

AJAX DEMO

Page 67: jQuery Learning

References:

•http://jquery.com/

•http://jqueryui.com/

•http://jquerymobile.com/

•http://programmers.stackexchange.com/questions/166273/advantages-of-using-pure-javascript-over-jquery

•http://www.slideshare.net/simon/jquery-in-15-minutes

•Internet