Jquery: The Way To Javascript and Rich Internet Applications
Jquery: The Way To Javascript and Rich Internet Applications
Jquery: The Way To Javascript and Rich Internet Applications
Introduction to jQuery
Developed by John Resig at Rochester Institute of Technology
jQuery is a lightweight JavaScript library that emphasizes
John Resig
John Resig is a JavaScript Tool Developer
Introduction to jQuery
Why do I want it
How do I get it
www.jquery.com
Introduction to jQuery
Installation You just download the
The DOM
Document Object Model
jQuery is DOM scripting
Heirarchal structure of a web page
You can add and subtract DOM elements
on the fly
You can change the properties and
contents of DOM elements on the fly
The DOM
The Document Object Model (DOM) is a cross-platform and
jQuery() = $()
$(function)
The Ready handler
$(selector)
Element selector expression
$(element)
Specify element(s) directly
$(HTML)
HTML creation
$.function()
Execute a jQuery function
$.fn.myfunc(){}
Create jQuery function
jquery(document).ready(function(){};);
jquery().ready(function(){};)
jquery(function(){};)
jquery(dofunc);
$(dofunc);
$(#id)
id of element
$(p)
tag name
$(.class)
CSS class
$(p.class) <p> elements having the CSS class
$(p:first) $(p:last) $(p:odd) $(p:even)
$(p:eq(2))
gets the 2nd <p> element (1 based)
$(p)[1]
gets the 2nd <p> element (0 based)
$(p:nth-child(3))
gets the 3rd <p> element of the parent. n=even, odd too.
$(p:nth-child(5n+1)) gets the 1st element after every 5th one
$(p a)
<a> elements, descended from a <p>
$(p>a)
<a> elements, direct child of a <p>
$(p+a)
<a> elements, directly following a <p>
$(p, a)
<p> and <a> elements
$(li:has(ul))
<li> elements that have at least one <ul> descendent
$(:not(p))
all elements but <p> elements
$(p:hidden)
only <p> elements that are hidden
$(p:empty)
<p> elements that have no child elements
.each()
iterate over the set
.size()
number of elements in set
.end()
reverts to the previous set
.get(n)
get just the nth element (0 based)
.eq(n)
get just the nth element (0 based) also .lt(n) & .gt(n)
.slice(n,m) gets only nth to (m-1)th elements
.not(p)
dont include p elements in set
.add(p)
add <p> elements to set
.remove() removes all the elements from the page DOM
.empty()
removes the contents of all the elements
.filter(fn/sel) selects elements where the func returns true or sel
.find(selector)
selects elements meeting the selector criteria
.parent()
returns the parent of each element in set
.children() returns all the children of each element in set
.next()
gets next element of each element in set
.prev()
gets previous element of each element in set
.siblings()
gets all the siblings of the current element
Adding Events
Mouseover events bind, hover, toggle
Button click events
Keystrokes
Event Background
DOM Level 2 Event Model
Multiple event handlers, or listeners, can be
established on an element
These handlers cannot be relied upon to run
an any particular order
When triggered, the event propagates from
the top down (capture phase) or bottom up
(bubble phase)
IE doesnt support the capture phase
$(img).unbind(click,imgclick());
$(img).unbind(click);
$(img).one(click,imgclick(event));
Only works once
$(img).click(imgclick);
$(img).toggle(click1, click2);
$(img).hover(mouseover, mouseout);
this
this.id
this.tagName
this.attr
this.src
this.classname
this.title
this.alt
this.value (for form elements)
Event properties
Event Methods
.stopPropagation()
.preventDefault()
.trigger(eventType)
no bubbling
no <a> link, no <form> submit
does not actually trigger the
event, but calls the appropriate function specified as the
one tied to the eventType
.click(), blur(), focus(), select(), submit()
With no parameter, invokes the event handlers, like
trigger does, for all the elements in the wrapped set
.click(func)
.submit(func)
.dblclick(func)
.mouseover(func)
.mouseout(func)
.select(func)
AJAX
What is AJAX
The basic AJAX function
XMLHttpRequest
Initiating a request
Getting the response