dosug intro to jquery javascript framework

33
SCOTT RYAN MAY 2008 [email protected] JQuery

Upload: matthew-mccullough

Post on 11-May-2015

4.074 views

Category:

Technology


1 download

DESCRIPTION

An Intro to the JQuery JavaScript Framework by Scott Damon Ryan of the Denver Open Source Users Group (DOSUG)

TRANSCRIPT

Page 1: DOSUG Intro to JQuery JavaScript Framework

S C O T T R Y A N M A Y 2 0 0 8

S C O T T @ T H E R Y A N S P L A C E . C O M

JQuery

Page 2: DOSUG Intro to JQuery JavaScript Framework

Agenda

 Web Development Introduction  JQuery Introduction  Selectors  Modifiers  Events  Animation  Ajax  Plugins

Page 3: DOSUG Intro to JQuery JavaScript Framework

Best Practices

 Separation of Concerns  HTML – Layout  CSS – Look and Feel   JavaScript – Event Handling and Dynamic Behavior  Ajax – Remote access and dynamic data

Page 4: DOSUG Intro to JQuery JavaScript Framework

Why JQuery

 Captures standard pattern  Select ,add or filter, manipulate, repeat  Unobtrusive JavaScript

 Table Striping Example

Page 5: DOSUG Intro to JQuery JavaScript Framework

Table Striping (Dom Code)

var tables = document.getElementsByTagName("table");

for ( var t = 0; t < tables.length; t++ ) { var rows = tables[t].getElementsByTagName("tr");

for ( var i = 1; i < rows.length; i += 2 ) if ( !/(^|s)odd(s|$)/.test( rows[i].className ) ) rows[i].className += " odd";

}

Page 6: DOSUG Intro to JQuery JavaScript Framework

Table Striping (Prototype)

$$("table").each(function(table) { Selector.findChildElements(table,

["tr"]) .findAll(function(row,i){ return i % 2 == 1; }) .invoke("addClassName", "odd"); });

Page 7: DOSUG Intro to JQuery JavaScript Framework

Table Striping (jQuery)

 $("tr:nth-child(odd)").addClass("odd");

Page 8: DOSUG Intro to JQuery JavaScript Framework

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

Page 9: DOSUG Intro to JQuery JavaScript Framework

Document Ready Handler

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

 Runs when DOM is loaded  Cross Browser  Don’t need to wait for resources

Page 10: DOSUG Intro to JQuery JavaScript Framework

Extending JQuery

 $.fn.samplefunc = function() { Return this.each( function(){code goes here}); }

 $(‘#sample’).samplefunc().addClass(‘NewClass’);

Page 11: DOSUG Intro to JQuery JavaScript Framework

Other Libraries

  jQuery.noConflict();

Page 12: DOSUG Intro to JQuery JavaScript Framework

Select

Page 13: DOSUG Intro to JQuery JavaScript Framework

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

Page 14: DOSUG Intro to JQuery JavaScript Framework

Positional Selectors

 :first  :last  :first-child  :last-child  :only-child  :nth-child(2)  :nth-child(even)

Page 15: DOSUG Intro to JQuery JavaScript Framework

Special Selectors

  :button   :checkbox   :checked   :contains(text string)   :enabled   :disabled   :input   :hidden

  :submit   :selected   :text   :visible

Page 16: DOSUG Intro to JQuery JavaScript Framework

Managing the Wrapped Set

 size  get(x)   index(element)  add(expression)  not(expression)  filter(expression)  Slice(begin, end)

Page 17: DOSUG Intro to JQuery JavaScript Framework

Selection Demo

 $(‘#hibiscus’)  $(‘img[alt],img[title]’)  $('img[alt]').add('img[title]')  $('li > a')  $('li > a:first')  $("#aTextField").attr("value","test")  $(‘input[type=text]’)  $(‘a[href$=.pdf]’)  $(‘div[title^=my]’)

Page 18: DOSUG Intro to JQuery JavaScript Framework

More Sample Selectors

 $(‘li:has(a)’);  $(‘li a’);

Page 19: DOSUG Intro to JQuery JavaScript Framework

Create/Filter/Manipulate

Page 20: DOSUG Intro to JQuery JavaScript Framework

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”);

Page 21: DOSUG Intro to JQuery JavaScript Framework

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”);

Page 22: DOSUG Intro to JQuery JavaScript Framework

More Manipulation

  addClass   removeClass   toggleClass   css(name,value)  width,height, css  hasClass, getClassNames  html, html(data)   text , text(data)

  append, appendTo  prepend, prependTo  before,insertBefore   after, insertAfter  wrap, wrapAll,wrapInner   remove   empty   replaceWith

(after.remove)

Page 23: DOSUG Intro to JQuery JavaScript Framework

Replacing Elements

 $(‘#divToReplace’)   .replaceWith(‘<p>This is new Data</p>’)   .remove();

Page 24: DOSUG Intro to JQuery JavaScript Framework

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)

Page 25: DOSUG Intro to JQuery JavaScript Framework

JQuery Event Model

 Unified Event Model  Unified Event Object  Supports Model 2 Semantics  Propagation

 Cascade  Bubble

Page 26: DOSUG Intro to JQuery JavaScript Framework

Event Semantics

 bind(eventType,data,listener)  eventTypeName(listener)  one(eventType, data,listener)  unbind(eventType, listener)  unbind(event)  trigger(eventType)  toggle(oddListener,evenListener)

Page 27: DOSUG Intro to JQuery JavaScript Framework

Simple Bind

 $(function(){ $(‘#sample’) .bind(‘click’,clickOne) .bind(‘click’,clickTwo) .bind(‘click’,clickThree) .bind(‘mouseover’,mouse);

Page 28: DOSUG Intro to JQuery JavaScript Framework

Event Sample (Toggle/Inline)

$(function(){ $(‘#sample’).toggle( function(event){ $(event.target).css(‘opacity’0.4);}, function(event){ $(event.target).css(‘opacity”,1.0;} ); });

Page 29: DOSUG Intro to JQuery JavaScript Framework

Event Sample (Hover/External)

$(‘#sample’) .bind(‘mouseover’ , report) .bind(‘mouseout’ , report);

function report (event) { $(‘#output’).append(‘<div>’+event.type+’</div>’); }

$(‘#sample’).hover(report , report);

Page 30: DOSUG Intro to JQuery JavaScript Framework

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

Page 31: DOSUG Intro to JQuery JavaScript Framework

JQuery Utility Functions

 browser, box model, float style flags  trim  each  grep   inArray, makeArray, unique, extend  getScript

Page 32: DOSUG Intro to JQuery JavaScript Framework

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

Page 33: DOSUG Intro to JQuery JavaScript Framework

Ajax

  load (url, parameters, callback)  serialize, serializeArray  get  getJSON  post  ajax