This document contains the slides from a presentation given by Mike Hostetler on December 6, 2011 titled "StarTechConf 2011". The presentation introduces jQuery, covering topics such as including jQuery, selecting elements, manipulating styles, chaining methods, DOM manipulation, and binding events. Examples are provided throughout to demonstrate jQuery syntax and functionality.
14. StarTechConf 2011
Your first jQuery
Su primera jQuery
@mikehostetler
Tuesday, December 6, 11
15. StarTechConf 2011
jQuery Function / Función de jQuery
jQuery( );
// That’s it
@mikehostetler
Tuesday, December 6, 11
16. StarTechConf 2011
The $
// $ is an allowed variable in JavaScript
window.jQuery = window.$ = jQuery;
$ === jQuery // TRUE
@mikehostetler
Tuesday, December 6, 11
17. StarTechConf 2011
Finding Something
Para encontrar algo
@mikehostetler
Tuesday, December 6, 11
18. StarTechConf 2011
Finding
Something
// Select By ID / Selección por ID
<div id=”foo”></div>
<div></div>
$(‘#foo’);
<div id=”foo”></div>
<div></div>
@mikehostetler
Tuesday, December 6, 11
19. StarTechConf 2011
Finding
Something
// Select by class / Selección por ‘class’
<div class=”myClass foo bar”></div>
<div class=”baz myClass”></div>
<div class=”bar”></div>
$(‘.myClass’)
<div class=”myClass foo bar”></div>
<div class=”baz myClass”></div>
<div class=”bar”></div>
@mikehostetler
Tuesday, December 6, 11
20. StarTechConf 2011
Finding
Something
// Select by tag / Selección por ‘tag’
<ul>
<li>Apple</li>
<li>Pear</li>
</ul>
$(“li”);
<ul>
<li>Apple</li>
<li>Pear</li>
</ul>
@mikehostetler
Tuesday, December 6, 11
21. StarTechConf 2011
Finding
Something
in
Context
Encontrar
algo
en
su
contexto
‣ Entire Document / Documento completo
$(‘div’)
‣ Scope your selector / Ámbito de su selección
$(‘selector’, <context>)
$(‘#table’).find(‘selector’)
‣ $(‘a’).click(function() {
$(‘span’, this)...
});
@mikehostetler
Tuesday, December 6, 11
22. StarTechConf 2011
Do Something
Hacer algo
@mikehostetler
Tuesday, December 6, 11
23. StarTechConf 2011
Do
Something
<p>One</p>
<p>Two</p>
<p>Three</p>
// Find Something / Encontrar algo
$(‘p’)
// Do Something / Hacer algo
$(‘p’).hide();
// Generic Syntax
$(‘p’) . <Method Name> ( [PARAMETERS] );
@mikehostetler
Tuesday, December 6, 11
24. StarTechConf 2011
Showing
and
Hiding
// HTML
<p>One</p>
<p>Two</p>
<p>Three</p>
// Show the elements / Mostrar los elementos
$(‘p’).show();
// Hide the elements / Ocultar los elementos
$(‘p’).hide();
@mikehostetler
Tuesday, December 6, 11
25. StarTechConf 2011
Showing
and
Hiding
// HTML
<p>One</p>
<p>Two</p>
<p>Three</p>
// Show the elements / Mostrar los elementos
$(‘p’).show();
// Hide the elements / Ocultar los elementos
$(‘p’).hide();
@mikehostetler
Tuesday, December 6, 11
26. StarTechConf 2011
Iteration
/
Iteración
<p>One</p>
<p>Two</p>
<p>Three</p>
$(‘p’).each(function() {
// Operates on each p individually
var rand;
rand = Math.floor( Math.random() * 1 );
if(rand > 1) {
$(this).hide();
}
});
@mikehostetler
Tuesday, December 6, 11
27. StarTechConf 2011
Iteration
/
Iteración
<p>One</p>
<p>Two</p>
<p>Three</p>
$(‘p’).each(function() {
// Operates on each p individually
var rand;
rand = Math.floor( Math.random() * 1 );
if(rand > 1) {
$(this).hide();
}
});
@mikehostetler
Tuesday, December 6, 11
28. StarTechConf 2011
Iteration
/
Iteración
<p>One</p>
<p>Two</p>
<p>Three</p>
$(‘p’).each(function() {
// Operates on each p individually
var rand;
rand = Math.floor( Math.random() * 1 );
if(rand > 1) {
$(this).hide();
}
});
@mikehostetler
Tuesday, December 6, 11
29. StarTechConf 2011
Styling
// HTML
<p>One</p>
<p class=”foo”>Two</p>
<p>Three</p>
$(‘p’).addClass(‘enabled’);
$(‘p’).removeClass(‘foo’);
$(‘p’).toggleClass(‘foo’);
@mikehostetler
Tuesday, December 6, 11
36. StarTechConf 2011
DOM Manipulation
Manipulación del DOM
@mikehostetler
Tuesday, December 6, 11
37. StarTechConf 2011
Creating
Elements
/
Creación
de
elementos
$("<div/>", {
class: "test",
text: "Click me!",
click: function(){
$(this).toggleClass("test");
}
});
// Clicking toggles the class / Al hacer clic cambia la ‘class’
<div class=”test”>Click me!</div>
@mikehostetler
Tuesday, December 6, 11
38. StarTechConf 2011
Inserting
Elements
/
Insertar
elementos
// Before / Antes
<p>Apple</p>
<p>Orange</p>
$(‘p’).after(‘<img src=”logo.png” />’);
// After / Después
<p>Apple</p>
<img src=”logo.png />
<p>Orange</p>
<img src=”logo.png />
@mikehostetler
Tuesday, December 6, 11
39. StarTechConf 2011
Inserting
Elements
/
Insertar
elementos
// Before / Antes
<p id=”apple”>Apple</p>
<p id=”orange”>Orange</p>
$(‘<img src=”apple.png” />’).prependTo(‘#apple’);
$(‘<img src=”orange.png” />’).appendTo(‘#orange’);
// After / Después
<p id=”apple”><img src=”apple.png” />Apple</p>
<p id=”orange”>Orange<img src=”orange.png” /></p>
@mikehostetler
Tuesday, December 6, 11
40. StarTechConf 2011
Removing
Elements
/
Remover
elementos
// Before / Antes
<div>
<p>Red</p>
<p>Green</p>
</div>
// Removing Elements Directly /
Eliminación de los elementos directamente.
$(‘p’).remove();
// After / Después
<div> </div>
@mikehostetler
Tuesday, December 6, 11
41. StarTechConf 2011
Wrapping
Elements
/
Envolver
elementos
// Before / Antes
<p>Hello world</p>
<p>Foo bar</p>
$(‘p’).wrap(‘<div></div>’);
// After / Después
<div><p>Hello world</p></div>
<div><p>Foo bar</p></div>
@mikehostetler
Tuesday, December 6, 11
42. StarTechConf 2011
Unwrapping
Elements
// Before / Antes
<div><p>Hello world</p></div>
<div><p>Foo bar</p></div>
// Individually / Individualmente
$(‘p’).unwrap();
// After / Después
<p>Hello world</p>
<p>Foo bar</p>
@mikehostetler
Tuesday, December 6, 11
48. StarTechConf 2011
Binding
Events
with
.bind()
// Binding an event / Definir Evento
$('a.tab').bind('click',function(e){
// event handling code
$(this).css(‘color’, ‘red’);
});
// Unbinding an event / Liberar Evento
$('a.tab').unbind('click');
@mikehostetler
Tuesday, December 6, 11
49. StarTechConf 2011
Binding
Events
// Bind an event to execute once /
Definar Evento que corra una sola vez
$('a.tab').one('click',function(e){
// event handling code
$(this).css(‘color’,‘red’);
});
@mikehostetler
Tuesday, December 6, 11
51. StarTechConf 2011
Making
a
Request
/
Hacer
un
pedido
$.get(‘page.php’, function(data) {
$(data).appendTo(‘body’);
}, ‘html’);
var data = { fname: ‘Jonathan’ };
$.post(‘page.php’, data, function(data) {
$(data).appendTo(‘body’);
}, ‘html’);
@mikehostetler
Tuesday, December 6, 11
52. StarTechConf 2011
Making
a
Request
with
JSON
Hacer
un
pedido
de
JSON
// Response / Respuesta
{“names”: [“Jonathan”, “Mike”, “Rob”],
“states”: {“NE” : “Nebraska”},
“year” : “2011” }
$.getJSON(‘page.php’, function(json) {
$(‘body’).append( json.names[0] );
$(‘body’).append( json.states.NE );
});
@mikehostetler
Tuesday, December 6, 11
53. StarTechConf 2011
Making
a
Request
JSON
// Response
{ “names”: [“Jonathan”, “Mike”, “Rob”] }
$.getJSON(‘page.php’, function(json) {
$.each(json.names, function(i, val) {
$(‘body’).append( val );
});
});
@mikehostetler
Tuesday, December 6, 11
54. StarTechConf 2011
Writing Your First Plugin
Escribe tu primer plugin
@mikehostetler
Tuesday, December 6, 11
56. StarTechConf 2011
Plugins
&
Iteration
/
Plugins
&
Iteración
$.fn.elementCount = function() {
// this is a jQuery Object
this.each(function(i) {
// this is the element
$(this).html($(this).html() +‘ ‘+ i);
});
};
$(‘p’).elementCount();
@mikehostetler
Tuesday, December 6, 11
57. StarTechConf 2011
Plugins
and
Chaining
// Add support for chaining
$.fn.elementCount = function() {
return this.each(function(i) {
$(this).html($(this).html() +‘ ‘+ i);
});
};
$(‘p’).elementCount().addClass(‘active’);;
@mikehostetler
Tuesday, December 6, 11
58. StarTechConf 2011
Plugins
and
Chaining
// Plugin is required to return this
$.fn.SantiagoAMil = function() {
return this.each(function(i) {
$(this).css(‘color’, ‘red’);
});
};
$(‘p’).SantiagoAMil().addClass(‘fiesta’);
@mikehostetler
Tuesday, December 6, 11
59. StarTechConf 2011
The Plugin Pattern
El patrón de Plugin
@mikehostetler
Tuesday, December 6, 11