Upload
andrew-homeyer
View
863
Download
0
Tags:
Embed Size (px)
DESCRIPTION
an introduction to the jQuery JavaScript framework, with comparison to Prototype/Scriptaculous
Citation preview
jQueryv. 1.3.2
Andrew Homeyerwith Near Infinity since July ‘08
twitter: @andrewhomeyer
ajaxrain.com
http://www.google.com/insights/search/#cat=422&q=jquery%2Cprototype&date=1%2F2006%2045m&cmpt=q
http://www.google.com/insights/search/#cat=422&q=jquery%2Cprototype%2Cmootools%2Cdojo%2CYUI&date=1%2F2006%2045m&cmpt=q
jQuery:
$(‘li:last’).css(‘backgroundColor’, ‘red’);
Prototype:
$$(‘li’).last().setStyle({‘backgroundColor’: ‘red’});
How it’s gonna go down:
$( )
jQuery( )
jQuery.noConflict();
Play nice with others
$(‘#mydiv’) //element with id mydiv
$(‘mydiv’)
Selecting elements
$(‘div’) //all div elements on page
$$(‘div’)
$$(‘div.warning’)
$(‘div.warning’) //all div elementswith class of warning
$$(‘*’)
$(‘*’) //everything
$$('span').findAll(function(v){ return v.select('img').length > 0;});
$(‘span:has(img)’) // all spans that contain an image
selecting with filters
$$(‘.mytable tr:nth-child(even)’)
$(‘.mytable tr:even’) //even rows
:first:last:not(selector):even:odd:eq(index)
:contains(text)
:empty:has(selector):parent:hidden:visible:checked:selected:input
:animated
:first:last:not(selector):even:odd:eq(index)
:contains(text)
:empty:has(selector):parent:hidden:visible:checked:selected:input
:animated
new Element(‘a’)
$(‘<a></a>’) //returns a new <a>
creating elements
document.body.insert(new Element(‘a’))
$(‘<a></a>’).appendTo(document.body)
document.body.insert(‘<a></a>’)
$(‘body’).append(‘<a></a>’)
$(‘li’) .css(‘backgroundColor’, ‘red’) .text(‘new content’)
$$(‘li’).each(function(v){ v.setStyle({‘backgroundColor’: ‘red’}).update(‘new content’);}
chaining
$(‘div’)
$(‘div’).filter(function(){ return $(this).css(‘color’) == ‘green’})
$(‘div’).filter(function(){ return $(this).css(‘color’) == ‘green’}) .remove()
$(‘div’).filter(function(){ return $(this).css(‘color’) == ‘green’}) .remove() .end()
$(‘div’).filter(function(){ return $(this).css(‘color’) == ‘green’}) .remove() .end().text(“no green divs”)
events
$(‘a’).click(function(){ $(this).slideUp();})
$$(‘a’).each(function(v){ v.observe(‘click’, function(a){ Effect.SlideUp(a); });});
effects
$(‘#box’).show(‘slow’);
$(‘box’).appear();
showhidetoggleslideDownslideUpslideToggle
fadeInfadeOutfadeToanimate
jQuery.fx.off = true;
short circuit effects
$.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})
$(‘#container’).load(‘content.html’)
ajax
new Ajax.Updater(‘container’, ‘content.html’);
ajax with JSONP
$.getJSON(url, function(data){ //process your data});
<script type=“text/javascript”>
$(document).ready(function(){ //DOM’s loaded, do your stuff});
</script>
document.observe(‘dom:loaded’, function(){
});
Waiting for the DOM
<script type=“text/javascript”>
$(function(){ //DOM’s loaded, do your stuff});
</script>
shortcut
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
referencesjQuery Enlightenment (http://jqueryenlightenment.com/)
API: docs.jquery.com, visualjquery.com