j query(04 12 2008) foiaz

17
JQuery 16/10/2008 04/12/2008

Upload: testingphase

Post on 06-May-2015

1.307 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: J Query(04 12 2008) Foiaz

JQuery

16/10/200804/12/2008

Page 2: J Query(04 12 2008) Foiaz

Agenda

• Client Side JavaScript

• Introduction to JQuery

• JQuery Selectors

• JQuery Filters

• JQuery Events

• Other JQuery Effects

Page 3: J Query(04 12 2008) Foiaz

What is JavaScript?

• An interpreted programming language with object oriented capabilities.

• Not Java!– Originally called LiveScript, changed to JavaScript as

a marketing ploy by Sun and Netscape.

• Not simple!– Although it is loosely typed and can be used by web

developers in a “cookbook” fashion, JavaScript is a fully featured programming language with many advanced features.

Page 4: J Query(04 12 2008) Foiaz

Client Side JavaScript

• When JavaScript is embedded in a web browser, it is referred to as Client Side JavaScript.

• Contains an extended set of functionality to interface with the web browser DOM (Document Object Model).

• Objects, such as window and document, and functions, like event detection and handling, are included in Client Side JavaScript.

Page 5: J Query(04 12 2008) Foiaz

JQuery

• Powerful JavaScript library– Simplify common JavaScript tasks– Access parts of a page

• using CSS or XPath-like expressions

– Modify the appearance of a page– Alter the content of a page– Change the user’s interaction with a page– Add animation to a page– Provide AJAX support

Page 6: J Query(04 12 2008) Foiaz

What is available with jQuery?

• Cross browser support and detection

• AJAX functions• CSS functions• DOM manipulation• DOM transversal• Attribute manipulation• Event detection and

handling

• JavaScript animation• Hundreds of plugins

for pre-built user interfaces, advanced animations, form validation, etc

• Expandable functionality using custom plugins

Page 7: J Query(04 12 2008) Foiaz

jQuery Syntax

$.func(…);

or

$(selector).func1(…).func2(…).funcN(…);

jQuery Object, can be used instead of jQuery

Selector syntax, many different selectors allowed

Chainable, most functions return a jQuery object

Function parameters

$

selector

func

(…)

Page 8: J Query(04 12 2008) Foiaz

Basic JQuery

• A JQuery object is a wrapper for a selected group of DOM nodes

• Selecting part of document is fundamental operation

• $() function is a factory method that creates JQuery objects

• $(“dt”) is a JQuery object containing all the “dt” elements in the document

Page 9: J Query(04 12 2008) Foiaz

Basic JQuery

• .addClass() method changes the DOM nodes by adding a ‘class’ attribute– The ‘class’ attribute is a special CSS construct that

provides a visual architecture independent of the element structures

• $(“li”).addClass(“emphasize”) will change all occurrences of <li> to <li class=“emphasize”>

• .removeClass(“emphasize”) will the remove the class.

Page 10: J Query(04 12 2008) Foiaz

Basic JQuery

• To make this change, put it in a function and call it when the document has been loaded and the DOM is createdfunction doEmph(){$(“dt”).addClass(“emphasize”)}

<body onLoad=“doEmph()”>

• We had to alter the HTML (bad)• Structure and appearance should be separated!• Also, onLoad waits until all images etc are

loaded. Tedious.

Page 11: J Query(04 12 2008) Foiaz

Basic JQuery

• JQuery provides an independent scheduling point after DOM is created and before images are loaded– $(document).ready(doEmph);

• No HTML mods required. All done in script.• Better solution:

– $(document).ready(function(){$(“dt”).addClass(“emphasize”)

});<html><head><script src="jquery.js" type="text/javascript"></script><script src="test.js" type="text/javascript"></script>…

Page 12: J Query(04 12 2008) Foiaz

JQuery Selectors

• CSSp element name

#id identifier

.class classname

p.class element with class

p a anchor as any descendant of p

p > a anchor direct child of p

Page 13: J Query(04 12 2008) Foiaz

jQuery Selector Examples• $( html )

– $(‘<p><a href=“index.html”>Click here!</a></p>’)

• $ ( elems )– $(document), $(window), $(this)

– $(document.getElementsByTagName(“p”))

• $ ( fn )– $(function() { alert(“Hello, World!”) });

• $ ( expr, context )– $(“p”), $(“form”), $(“input”)

– $(“p#content”), $(“#content”), $(“.brandnew”), $(“p span.brandnew:first-child, #content”)

Page 14: J Query(04 12 2008) Foiaz

JQuery Filters

p:first first paragraphli:last last list itema:nth(3) fourth linka:eq(3) fourth linkp:even or p:odd every other paragrapha:contains(‘click’) links that contain the

word click

Page 15: J Query(04 12 2008) Foiaz

JQuery Events

• bind(eventname, function) method – ‘click’– ‘change’– ‘resize’

• $(“a[@href]”).bind(‘click’,function(){$(this).addClass(‘red’);});

Page 16: J Query(04 12 2008) Foiaz

Other JQuery Effects

• .css(‘property’, ‘value’)• .css({‘prop1’:‘value1’, ‘prop2’:’value2’…})• E.g. .css(‘color’, ‘red’)

• .hide(speed) or .show(speed)– Where speed is ‘slow’, ‘normal’ or ‘fast’

Page 17: J Query(04 12 2008) Foiaz

• Live Demo