Beruflich Dokumente
Kultur Dokumente
http://trends.builtwith.com/javascript/jquery
Who's using jQuery
http://trends.builtwith.com/javascript/jquery
What exactly is jQuery
jQuery is a JavaScript library!
• JavaScript Events
• Animations
• Ajax interactions
What does that mean?
It means no more of this
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';
}
}
}
jQuery simplifies
jQuery('table tr:nth-child(odd)').addClass('odd');
jQuery simplifies
jQuery function
jQuery('table tr:nth-child(odd)').addClass('odd');
jQuery simplifies
jQuery function
jQuery('table tr:nth-child(odd)').addClass('odd');
CSS expression
jQuery simplifies
CSS expression
jQuery simplifies
jQuery('table tr:nth-child(odd)').addClass('odd');
It really is the
•jQuery(‘div’)
CSS selectors & custom CSS expressions
& jQuery(‘:first’)
• HTML
• DOM elements
• A function (shortcut for DOM ready)
jQuery(document) or jQuery(document.createElement(‘a’))
•jQuery(‘div’)
CSS selectors & custom CSS expressions
& jQuery(‘:first’)
•jQuery(‘<li><a
HTML
href=”#”>link</a></li>’)
• DOM elements
• A function (shortcut for DOM ready)
jQuery(function(){}) =
jQuery(document).ready(function(){})
•jQuery(‘div’)
CSS selectors & custom CSS expressions
& jQuery(‘:first’)
•jQuery(‘<li><a
HTML
href=”#”>link</a></li>’)
• DOM elements
jQuery(document) or jQuery(document.createElement(‘a’))
•jQuery(‘<li><a
HTML
href=”#”>link</a></li>’)
• DOM elements
jQuery(document) or jQuery(document.createElement(‘a’))
• Selectors
• Attributes
• Traversing
• Manipulation
• CSS
• Events
• Effects
• Ajax
• Utilities
• Core jQuery()
• Selectors each()
size()
• Attributes eq()
get()
• Traversing index()
• Manipulation length
selector
• CSS context
• Events data()
removeData()
• Effects queue()
dequeue()
• Ajax jQuery.fn.extend()
• Utilities jQuery.extend()
jQuery.noConflict()
• Core jQuery()
• Selectors each()
size()
• Attributes eq()
get()
• Traversing index()
• Manipulation length
selector
• CSS context
• Events data()
removeData()
• Effects queue()
dequeue()
• Ajax jQuery.fn.extend()
• Utilities jQuery.extend()
jQuery.noConflict()
• Core <!DOCTYPE html>
• Selectors
<html>
<body>
• Attributes
<p>Element Node</p>
• Traversing
•
<script src="jquery.js">
Manipulation </script>
• CSS <script>
alert($('p').get(0).nodeName);
• Events </script>
• Effects </body>
• Ajax </html>
• Utilities http://jsbin.com/ibito/edit
• Core <!DOCTYPE html>
• Selectors
<html>
<body>
• Attributes
<p>Element Node</p>
• Traversing
•
<script src="jquery.js">
Manipulation </script>
• CSS <script>
alert($('p').get(0).nodeName);
• Events alert($('p')[0].nodeName);
</script>
• Effects
• Ajax </body>
</html>
• Utilities http://jsbin.com/idiyi/edit
• Core
• Selectors
• Attributes
• Traversing
• Manipulation
• CSS
• Events
• Effects
• Ajax
• Utilities
• Core $(‘#nav li.contact’)
• Selectors
• Attributes
• Traversing
• Manipulation
• CSS
• Events
• Effects
• Ajax
• Utilities
• Core $(‘#nav li.contact’)
• Selectors $(‘:visible’)
• Attributes
• Traversing
• Manipulation
• CSS
• Events
• Effects
• Ajax
• Utilities
• Core $(‘#nav li.contact’)
• Selectors $(‘:visible’)
• Attributes $(‘:radio:enabled:checked’)
• Traversing
• Manipulation
• CSS
• Events
• Effects
• Ajax
• Utilities
• Core $(‘#nav li.contact’)
• Selectors $(‘:visible’)
• Attributes $(‘:radio:enabled:checked’)
• Traversing
$(‘a[title]’)
• Manipulation
• CSS
• Events
• Effects
• Ajax
• Utilities
• Core $(‘#nav li.contact’)
• Selectors $(‘:visible’)
• Attributes $(‘:radio:enabled:checked’)
• Traversing
$(‘a[title]’)
• Manipulation
$(‘a[title][hash*=”foo”]’)
• CSS
• Events
• Effects
• Ajax
• Utilities
• Core $(‘#nav li.contact’)
• Selectors $(‘:visible’)
• Attributes $(‘:radio:enabled:checked’)
• Traversing
$(‘a[title]’)
• Manipulation
$(‘a[title][hash*=”foo”]’)
• CSS
$(‘a:first[hash*=”foo”]’)
• Events
• Effects
• Ajax
• Utilities
• Core $(‘#nav li.contact’)
• Selectors $(‘:visible’)
• Attributes $(‘:radio:enabled:checked’)
• Traversing
$(‘a[title]’)
• Manipulation
$(‘a[title][hash*=”foo”]’)
• CSS
$(‘a:first[hash*=”foo”]’)
• Events
$(‘.header, .footer’)
• Effects
• Ajax
• Utilities
• Core $(‘#nav li.contact’)
• Selectors $(‘:visible’)
• Attributes $(‘:radio:enabled:checked’)
• Traversing
$(‘a[title]’)
• Manipulation
$(‘a[title][hash*=”foo”]’)
• CSS
$(‘a:first[hash*=”foo”]’)
• Events
$(‘.header, .footer’)
• Effects
$(‘.header, .footer’, ‘#main’)
• Ajax
• Utilities
• Core $(‘#nav li.contact’)
• Selectors $(‘:visible’)
• Attributes $(‘:radio:enabled:checked’)
• Traversing
$(‘a[title]’)
• Manipulation
$(‘a[title][hash*=”foo”]’)
• CSS
$(‘a:first[hash*=”foo”]’)
• Events
$(‘.header, .footer’)
• Effects
$(‘.header, .footer’, ‘#main’)
• Ajax
http://codylindley.com/jqueryselectors
• Utilities
• Core $(‘#nav li.contact’)
• Selectors $(‘:visible’)
• Attributes $(‘:radio:enabled:checked’)
• Traversing s e l e c t o r s
j Q u e r y
$(‘a[title]’)
• Manipulation
i l s i l e n t l y,
• fa $(‘a[title][hash*=”foo”]’)
s !
CSS
e C S S d o e
• Events just l i k
$(‘a:first[hash*=”foo”]’)
$(‘.header, .footer’)
• Effects
$(‘.header, .footer’, ‘#main’)
• Ajax
http://codylindley.com/jqueryselectors
• Utilities
• Core attr()
•
removeAttr()
Selectors
• Attributes
addClass()
hasClass()
• Traversing toggleClass()
removeClass()
• Manipulation
val()
• CSS
• Events
• Effects
• Ajax
• Utilities
• Core attr()
•
removeAttr()
Selectors
• Attributes
addClass()
hasClass()
• Traversing toggleClass()
removeClass()
• Manipulation
val()
• CSS
• Events
• Effects
• Ajax
• Utilities
• Core <!DOCTYPE html><html><body>
• Traversing $('input').focus(function(){
• Manipulation
var v = $(this).val();
$(this).val(
•
v === this.defaultValue ? '' : v
CSS );
•
}).blur(function(){
Events var v = $(this).val();
$(this).val(
• Effects
);
$.trim(v) ? v : this.defaultValue
• Ajax });
• Utilities </script></body></html>
http://jsbin.com/akeqo3/edit
• Core add() eq()
•
children() filter()
Selectors closest() is()
• Attributes
contents()
find()
map()
not()
• Traversing next()
nextAll()
slice()
• Manipulation offsetParent()
parent()
• CSS parents()
prev()
• Events prevAll()
siblings()
• Effects
andSelf()
• Ajax end()
• Utilities
• Core add() eq()
• Selectors
children() filter()
closest() is()
• Searches
Attributes
down
contents()
find()
map()
not()
• Traversing next()
nextAll()
slice()
• Manipulation offsetParent()
parent()
• CSS parents()
prev()
• Events prevAll()
siblings()
• Effects andSelf()
• Ajax end()
• Utilities
• Core add()
Filters across
eq()
•
children() filter()
Selectors closest() is()
• Attributes
contents()
find()
map()
not()
• Traversing next()
nextAll()
slice()
• Manipulation offsetParent()
parent()
• CSS parents()
prev()
• Events prevAll()
siblings()
• Effects
andSelf()
• Ajax end()
• Utilities
• Core add() eq()
•
children() filter()
Selectors closest() is()
• Attributes
contents()
find()
map()
not()
• Traversing next()
nextAll()
slice()
• Manipulation offsetParent()
parent()
• CSS parents()
prev()
• Events prevAll()
siblings()
• Effects
andSelf()
• Ajax end()
• Utilities
• Core <!DOCTYPE html>
•
<html>
Selectors <body>
• Traversing
<script src="jquery.js">
• Manipulation </script>
<script>
• CSS
$('p')
• Events .contents()
.wrap('<strong />');
• Effects
</script>
• Ajax </body>
•
</html>
Utilities http://jsbin.com/owesu/edit
• Core html() replaceWith()
•
text() replaceAll()
Selectors
• Attributes
append()
appendTo()
empty()
remove()
• Traversing prepend()
prependTo() clone()
• Manipulation
after()
• CSS before()
insert()
• Events insertAfter()
insertBefore
• Effects
wrap()
• Ajax wrapAll()
•
wrapInner()
Utilities
• Core html() replaceWith()
•
text() replaceAll()
Selectors
• Attributes
append()
appendTo()
empty()
remove()
• Traversing prepend()
prependTo() clone()
• Manipulation
after()
• CSS before()
insert()
• Events insertAfter()
insertBefore
• Effects
wrap()
• Ajax wrapAll()
•
wrapInner()
Utilities
• Core <!DOCTYPE html>
• Selectors
<html>
<body>
• Attributes
<p>jQuery’s <em>easy!</em></p>
• Traversing
•
<script src="jquery.js">
Manipulation </script>
• CSS <script>
• Events alert($(‘p’).text());
• Effects </script>
• Ajax </body>
</html>
• Utilities http://jsbin.com/inulu/edit
• Core css()
• Selectors offset()
• Attributes
offsetParent()
position()
• Traversing scrollTop()
scrollLeft()
• Manipulation
height()
• CSS width()
innerHeight()
• Events innerWidth()
outerHeight()
• Effects outerWidth()
• Ajax
• Utilities
• Core css()
• Selectors offset()
• Attributes
offsetParent()
position()
• Traversing scrollTop()
scrollLeft()
• Manipulation
height()
• CSS width()
innerHeight()
• Events innerWidth()
outerHeight()
• Effects outerWidth()
• Ajax
• Utilities
• Core <!DOCTYPE html>
<html>
• Selectors <head>
<style>
•
div { background: #ccc; width: 100px;
Attributes margin: 0 20px; float: left; }
</style>
• Traversing </head>
<body>
• Manipulation <div></div>
<div></div>
• CSS <div></div>
• Events
<script src=“jquery.js">
</script>
•
<script>
Effects
$('div').height($(document).height());
• Ajax </script>
•
</body>
Utilities </html> http://jsbin.com/erire3/edit
• Core ready() blur()
•
change()
Selectors bind() click()
• Attributes
one()
trigger()
dbclick()
focus()
•
error() submit()
Utilities
• Core ready() blur()
• Selectors
change()
bind() click()
• Acts
Attributes
on first, and
one()
trigger()
dbclick()
focus()
• Traversing
doesn’t chain! triggerHandler() keydown()
unbind() keypress()
• Manipulation live()
keyup()
mousedown()
• CSS die() mousenter()
mouseleave()
• Events hover()
toggle()
mouseout()
mouseup()
• Effects load()
resize()
scroll()
• Ajax unload() select()
• Utilities
error() submit()
• Core ready() blur()
IE fires on blur
•
change()
Selectors bind() click()
• Attributes
one()
trigger()
dbclick()
focus()
•
error() submit()
Utilities
• Core ready() blur()
•
change()
Selectors bind() click()
• Attributes
one()
trigger()
dbclick()
focus()
•
error() submit()
Utilities
• Core <!DOCTYPE html>
•
<html>
Selectors <body>
•
</script>
Ajax </body>
• Utilities </html>
http://jsbin.com/ogahu/edit
• Core <!DOCTYPE html>
•
<html>
Selectors <body>
• Traversing
<p>2. click</p>
• CSS
$(‘p’).live(‘click’, function(){
• Events $(this).after(‘<p>’ +
➥ $(this).text()+‘ clicked</p>’);
• Effects });
• Ajax </script>
</body>
• Utilities </html> http://jsbin.com/ihalu/edit
• Core show()
• Selectors
hide()
toggle()
• Attributes
slideDown()
• Traversing slideUp()
slideToggle()
• Manipulation
• CSS fadeIn()
fadeOut()
• Events fadeTo()
• Effects animate()
•
stop()
Ajax
• Utilities
• Core show()
• Selectors
hide()
toggle()
• Attributes
slideDown()
• Traversing slideUp()
slideToggle()
• Manipulation
• CSS fadeIn()
fadeOut()
• Events fadeTo()
• Effects animate()
•
stop()
Ajax
• Utilities
• Core <!DOCTYPE html><html><head>
<style>
• Selectors div {background:#bca; width:100px;
border:1px solid green;}
• Attributes </style>
</head>
• Traversing <body>
<div id="block">Hello!</div>
• Manipulation <script src="jquery.js"></script>
<script>
• CSS
$(‘#block’).animate({
• Ajax
! borderWidth: ‘10px’
}, 1500);
http://jsbin.com/oroto/edit
• Utilities </script></body></html>
• Core $.ajax() ajaxCompete()
• Selectors
$.get()
$.post()
ajaxError()
ajaxSend()
• Attributes $.getJSON()
$.getScript()
ajaxStart()
ajaxStop()
• Traversing $.ajaxSetup() ajaxSuccess()
• Manipulation load()
• CSS
serialize()
• Events serializeArray()
• Effects
• Ajax
• Utilities
• Core $.ajax() ajaxCompete()
• Selectors
$.get()
$.post()
ajaxError()
ajaxSend()
• Attributes $.getJSON()
$.getScript()
ajaxStart()
ajaxStop()
• Traversing $.ajaxSetup() ajaxSuccess()
• Manipulation load()
• CSS
serialize()
• Events serializeArray()
• Effects
• Ajax
• Utilities
• Core <!DOCTYPE html><html><body>
•
<script src="jquery.js">
Selectors </script>
• Attributes
<script>
• Traversing $.getJSON("http://twitter.com/
statuses/user_timeline.json?
• Manipulation screen_name=rem&callback=?",
function(data){
• CSS $.each(data,function(i,tweet){
$('<p/>').html
• Events (tweet.text).appendTo('body');
if ( i == 30 ) return false;
• Effects });
•
});
Ajax </script></body></html>
• Utilities http://jsbin.com/acisi/edit
• Core $.support $.isArray()
• Selectors
$.boxModel
$.browser
$.isFunction()
• Attributes
$.each()
$.trim()
$.param()
• Traversing $.extend()
$.grep()
• Manipulation $.makeArray()
• CSS $.map()
$.inArray()
• Events $.merge()
$.unique()
• Effects
• Ajax
• Utilities
• Core $.support $.isArray()
• Selectors
$.boxModel
$.browser
$.isFunction()
• Attributes
$.each()
$.trim()
$.param()
• Traversing $.extend()
$.grep()
• Manipulation $.makeArray()
• CSS $.map()
$.inArray()
• Events $.merge()
$.unique()
• Effects
• Ajax
• Utilities
• Core $.each(data, function(i, tweet){
•
$('<p />')
Selectors .html(tweet.text)
• Attributes
.appendTo('body');
• Traversing if ( i == 30 ) {
return false;
• Manipulation }
});
• CSS
• Events
• Effects
• Ajax
• Utilities http://jsbin.com/acisi/edit
Plugins
What’s a plugin?
A plugin is nothing more than a custom
jQuery method created to extend the
functionality of the jQuery object
$(‘ul’).myPlugin()
Why create a plugin?
})(jQuery);
</script></body></html>
Step 2:
attach plugin to fn
alias (aka prototype)
<!DOCTYPE html><html><body>
<script src=”jquery.js”></script>
<script>
(function ($) {
$.fn.notHate = function () {
$(this).text(
$(this).text().replace(/hate/g, ‘love’)
);
}; // end of plugin
})(jQuery);
</script></body></html>
<!DOCTYPE html><html><body>
<p>I hate jQuery!</p>
<script src=”jquery.js”></script>
<script>
(function ($) {
$.fn.notHate = function () {
$(this).text(
$(this).text().replace(/hate/g, ‘love’)
);
}; // end of plugin
})(jQuery);
$(‘p’).notHate();
</script></body></html>
Step 3:
add implicit iteration
<!DOCTYPE html><html><body>
<p>I hate jQuery!</p>
<p>I mean really hate jQuery!</p>
<script src=”jquery.js”></script>
<script>
(function ($) {
$.fn.notHate = function () {
this.each(function () {
$(this).text(
$(this).text().replace(/hate/g, ‘love’)
);
});
}; // end of plugin
})(jQuery);
$(‘p’).notHate();
</script></body></html>
Step 4:
enable chaining
<!DOCTYPE html><html><body>
<p>I hate jQuery!</p>
<p>I mean really hate jQuery!</p>
<script src=”jquery.js”></script>
<script>
(function ($) {
$.fn.notHate = function () {
return this.each(function () {
$(this).text(
$(this).text().replace(/hate/g, ‘love’)
);
});
}; // end of plugin
})(jQuery);
$(‘p’).notHate().hide();
</script></body></html>
Step 5:
add default options
<!DOCTYPE html><html><body>
<p>I hate jQuery!</p>
<p>I mean really hate jQuery!</p>
<script src=”jquery.js”></script>
<script>
(function ($) {
$.fn.notHate = function () {
return this.each(function () {
$(this).text(
$(this).text().replace(/hate/g,
➥ $.fn.notHate.defaults.text)
);
});
}; // end of plugin
$.fn.notHate.defaults = {text:‘love’};
})(jQuery);
$(‘p’).notHate();
</script></body></html>
Step 6:
add custom options
<!DOCTYPE html><html><body>
<p>I hate jQuery!</p>
<p>I mean really hate jQuery!</p>
<script src=”jquery.js”></script>
<script>
(function ($) {
$.fn.notHate = function () {
return this.each(function () {
$(this).text(
$(this).text().replace(/hate/g,
➥ $.fn.notHate.defaults.text)
);
});
}; // end of plugin
$.fn.notHate.defaults = {text:‘love’};
})(jQuery);
$(‘p’).notHate({text: ‘love-love-love’});
</script></body></html>
<!DOCTYPE html><html><body>
<p>I hate jQuery!</p>
<p>I mean really hate jQuery!</p>
<script src=”jquery.js”></script>
<script>
(function ($) {
$.fn.notHate = function (options) {
return this.each(function () {
$(this).text(
$(this).text().replace(/hate/g,
➥ $.fn.notHate.defaults.text)
);
});
}; // end of plugin
$.fn.notHate.defaults = {text:‘love’};
})(jQuery);
$(‘p’).notHate({text: ‘love-love-love’});
</script></body></html>
<!DOCTYPE html><html><body>
<p>I hate jQuery!</p>
<p>I mean really hate jQuery!</p>
<script src=”jquery.js”></script>
<script>
(function ($) {
$.fn.notHate = function (options) {
var settings = $.extend({},
➥ $.fn.notHate.defaults, options);
return this.each(function () {
$(this).text(
$(this).text().replace(/hate/g,
➥ $.fn.notHate.defaults.text)
);
});
}; // end of plugin
$.fn.notHate.defaults = {text:‘love’};
})(jQuery);
$(‘p’).notHate({text: ‘love-love-love’});
</script></body></html>
<!DOCTYPE html><html><body> http://jsbin.com/ifuga/edit
<p>I hate jQuery!</p>
<p>I mean really hate jQuery!</p>
<script src=”jquery.js”></script>
<script>
(function ($) {
$.fn.notHate = function (options) {
var settings = $.extend({},
➥ $.fn.notHate.defaults, options);
return this.each(function () {
$(this).text(
$(this).text().replace(/hate/g,
➥ settings.text)
);
});
}; // end of plugin
$.fn.notHate.defaults = {text:‘love’};
})(jQuery);
$(‘p’).notHate({text: ‘love-love-love’});
</script></body></html>
Plugins are simple,
just follow those steps.
News
• Four conferences next year:
London, Boston, San Francisco and online
• New plugin site
• jQuery Forum (moving from Google Groups)
• jQuery.org & Foundation (Software Freedom
Law Centre)
• Infrastructure upgrade
Remy Sharp @rem
jqueryfordesigners.com
remysharp.com
jqueryfordesigners.com
?
Questions
remysharp.com