39
jQuery v. 1.3.2

jQuery

Embed Size (px)

DESCRIPTION

an introduction to the jQuery JavaScript framework, with comparison to Prototype/Scriptaculous

Citation preview

Page 1: jQuery

jQueryv. 1.3.2

Page 2: jQuery

Andrew Homeyerwith Near Infinity since July ‘08

twitter: @andrewhomeyer

Page 3: jQuery
Page 4: jQuery
Page 5: jQuery

ajaxrain.com

Page 8: jQuery

jQuery:

$(‘li:last’).css(‘backgroundColor’, ‘red’);

Prototype:

$$(‘li’).last().setStyle({‘backgroundColor’: ‘red’});

How it’s gonna go down:

Page 9: jQuery

$( )

Page 10: jQuery

jQuery( )

Page 11: jQuery

jQuery.noConflict();

Play nice with others

Page 12: jQuery

$(‘#mydiv’) //element with id mydiv

$(‘mydiv’)

Selecting elements

Page 13: jQuery

$(‘div’) //all div elements on page

$$(‘div’)

Page 14: jQuery

$$(‘div.warning’)

$(‘div.warning’) //all div elementswith class of warning

Page 15: jQuery

$$(‘*’)

$(‘*’) //everything

Page 16: jQuery

$$('span').findAll(function(v){ return v.select('img').length > 0;});

$(‘span:has(img)’) // all spans that contain an image

selecting with filters

Page 17: jQuery

$$(‘.mytable tr:nth-child(even)’)

$(‘.mytable tr:even’) //even rows

Page 18: jQuery

:first:last:not(selector):even:odd:eq(index)

:contains(text)

:empty:has(selector):parent:hidden:visible:checked:selected:input

:animated

Page 19: jQuery

:first:last:not(selector):even:odd:eq(index)

:contains(text)

:empty:has(selector):parent:hidden:visible:checked:selected:input

:animated

Page 20: jQuery

new Element(‘a’)

$(‘<a></a>’) //returns a new <a>

creating elements

Page 21: jQuery

document.body.insert(new Element(‘a’))

$(‘<a></a>’).appendTo(document.body)

Page 22: jQuery

document.body.insert(‘<a></a>’)

$(‘body’).append(‘<a></a>’)

Page 23: jQuery

$(‘li’) .css(‘backgroundColor’, ‘red’) .text(‘new content’)

$$(‘li’).each(function(v){ v.setStyle({‘backgroundColor’: ‘red’}).update(‘new content’);}

chaining

Page 24: jQuery

$(‘div’)

Page 25: jQuery

$(‘div’).filter(function(){ return $(this).css(‘color’) == ‘green’})

Page 26: jQuery

$(‘div’).filter(function(){ return $(this).css(‘color’) == ‘green’}) .remove()

Page 27: jQuery

$(‘div’).filter(function(){ return $(this).css(‘color’) == ‘green’}) .remove() .end()

Page 28: jQuery

$(‘div’).filter(function(){ return $(this).css(‘color’) == ‘green’}) .remove() .end().text(“no green divs”)

Page 29: jQuery

events

$(‘a’).click(function(){ $(this).slideUp();})

$$(‘a’).each(function(v){ v.observe(‘click’, function(a){ Effect.SlideUp(a); });});

Page 30: jQuery

effects

$(‘#box’).show(‘slow’);

$(‘box’).appear();

Page 31: jQuery

showhidetoggleslideDownslideUpslideToggle

fadeInfadeOutfadeToanimate

Page 32: jQuery

jQuery.fx.off = true;

short circuit effects

Page 33: jQuery

$.ajax({ url:'ajax.json', dataType: 'json', error: function(xhr, textStatus, errorThrown){ console.log(textStatus); }, success: function(data, textStatus){ console.log(data); }});

ajax

new Ajax.Request(url, {options})

Page 34: jQuery

$(‘#container’).load(‘content.html’)

ajax

new Ajax.Updater(‘container’, ‘content.html’);

Page 35: jQuery

ajax with JSONP

$.getJSON(url, function(data){ //process your data});

Page 36: jQuery

<script type=“text/javascript”>

$(document).ready(function(){ //DOM’s loaded, do your stuff});

</script>

document.observe(‘dom:loaded’, function(){

});

Waiting for the DOM

Page 37: jQuery

<script type=“text/javascript”>

$(function(){ //DOM’s loaded, do your stuff});

</script>

shortcut

Page 38: jQuery

plugins

jQuery.fn.pluck = function(attribute){ var plucked = []; this.each(function(){ plucked.push($(this).attr(attribute)); }); return plucked; };

http://plugins.jquery.com/many great ones out there, and if it’s not, write your own

Page 39: jQuery

referencesjQuery Enlightenment (http://jqueryenlightenment.com/)

API: docs.jquery.com, visualjquery.com