3. Best Practices
Separation of Concerns
HTML – Layout
CSS – Look and Feel
JavaScript – Event Handling and Dynamic Behavior
Ajax – Remote access and dynamic data
4. Why JQuery
Captures standard pattern
Select ,add or filter, manipulate, repeat
Unobtrusive JavaScript
Table Striping Example
5. Table Striping (Dom Code)
var tables =
document.getElementsByTagName(quot;tablequot;);
for ( var t = 0; t < tables.length; t++ )
{
var rows = tables[t].getElementsByTagName(quot;trquot;);
for ( var i = 1; i < rows.length; i += 2 ) if ( !/(^|
s)odd(s|$)/.test( rows[i].className ) )
rows[i].className += quot; oddquot;;
}
8. JQuery Wrapper
$(selector) or jQuery(selector)
Returns an array of Dom elements
Includes many methods
Example
$(“div.fademeout”).fadeOut();
Can be chained and always return a new array of
elements
Supports CSS selectors and custom selectors
9. Document Ready Handler
$(document).ready(function(){});
$(function(){});
Runs when DOM is loaded
Cross Browser
Don’t need to wait for resources
13. Selectors
Generic
Element Name (a, p, img, tr, etc)
ID (#theId)
Class (.theclassname)
a#theId.theclassname
p a.theclassname
Parent – Child
ul.myList > li > a
20. Creating HTML
$(“<div>Hello</div>”) or $(“<div>”)
$(“<div class=‘foo’>I have Foo</div><div>I Don’t</
div>”)
.filter(“.foo”)
.click(function(){alert (‘I am Foo’);})
.end()
.appendTo(“#somedivoutthere”);
21. DOM Manipulation
Each accesses every element in the wrapped set
Attr get and set values
Can use json syntax
Attr(title:’test’, value:’yes’)
removeAttr
$(“a[href^http://]”).attr(“target”,”_blank”);
24. Events and Event Model
Dom Level 0 Event Model
Single Events
Event Class (Proprietary)
Dom Level 2 Event Model
Multi Event
Event Class
Non IE
IE Event Model
Propagation (Bubble and Capture)
25. JQuery Event Model
Unified Event Model
Unified Event Object
Supports Model 2 Semantics
Propagation
Cascade
Bubble
30. Animation and Effects
show (speed, callback)
hide(speed, callback)
toggle(speed, callback)
Callback is a function that is passed a reference to this as the
calling element.
fadeIn, fadeOut, fadeTo
slideDown, slideUp, slideToggle
Custom animations
32. Plugins
Complex extensions
Should be developed to work with other libraries
Custom Utility functions
Custom wrapped methods
Form, Dimensions, Live Query, UI, MarkitUp
Beware of the $ but not too timid
Naming jquery.pluginname.js
Parameter Tricks
33. Ajax
load (url, parameters, callback)
serialize, serializeArray
get
getJSON
post
ajax