02.06.2013 Views

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Traversing the DOM<br />

In order to get the most done while writing the least<br />

amount of code, <strong>jQuery</strong> lets you chain functions together.<br />

Chaining is discussed on page 137, but in a nutshell it lets<br />

you select some pages elements, do one thing to them,<br />

then do another and another thing to them, simply by adding<br />

one function after another. For example, if you wanted<br />

to select all the paragraphs on a page and have them fade<br />

out of view then fade back into view, you could write this<br />

code:<br />

$(‘p’).fadeOut(500).fadeIn(500);<br />

You can chain as many functions together as you’d like<br />

including the DOM Traversal methods discussed above.<br />

For example, you could select a tag, add an outline<br />

around it, and then select all of the tags inside that<br />

and change the color of their text like this:<br />

$('div').css('outline','2px red solid').<br />

find('a').css('color','purple');<br />

Broken into pieces, this means:<br />

1. $(‘div’) selects all tags.<br />

2. .css(‘outline’,’2px red solid’) adds a 2-pixel red outline<br />

to the div.<br />

3. .find(‘a’) then changes the selection from the div to<br />

all of the tags inside the div.<br />

4. .css(‘color’,’purple’) makes the text of all of the links<br />

(not the div tag) purple.<br />

When adding one of the DOM Traversal functions to the<br />

chain, you alter the selection. For example, in the above<br />

code, <strong>jQuery</strong> first selects the div, then halfway through<br />

the chain, changes the selection to links inside the div. But<br />

sometimes when you want to return the selection to its<br />

original state. In other words, you want to select one thing,<br />

then select another thing in relation to the first selection,<br />

then return to the first selection. For example, say when a<br />

visitor clicks a div that has an opacity of 50%, you want to<br />

make the div fade to 100% opacity, change the color of the<br />

headline inside the div, add a background color to each p<br />

POWER USERS’ CLINIC<br />

Putting an .end() to DOM Traversal<br />

418 javascript & jquery: the missing manual<br />

inside the div. One action—a click—needs to trigger several<br />

actions on different elements of the page. One way to do<br />

this would be like this:<br />

$('div').click(function() {<br />

$(this).fadeTo(250,1); // fade div in<br />

$(this).find('h2').css('color','#F30');<br />

$(this).find('p').<br />

('backgroundColor','#F343FF');<br />

}); // end click<br />

Here’s a case where chaining would be really helpful—instead<br />

of calling $(this) three times, you could call it once<br />

and chain together the functions. However, you’d run into<br />

trouble if you tried to chain the functions like this:<br />

$('div').click(function() {<br />

$(this).fadeTo(250,1)<br />

.find('h2').css('color','#F30')<br />

.find('p').('backgroundColor','#F343FF');<br />

}); // end click<br />

This might look right, but the problem occurs after the<br />

.find(‘h2’)—which changes the selection from the div to<br />

the h2 tag inside the div. When the next .find() function<br />

runs—.find(‘p’)—<strong>jQuery</strong> tries to find p tags inside the h2 tag,<br />

not inside the div. Fortunately, you can use <strong>jQuery</strong>’s .end()<br />

function to “rewind” a changed selection back to its original<br />

state. In the example above, you can use .end() to return<br />

the selection back to the div, and then find the tags<br />

inside the div like this.<br />

$('div').click(function() {<br />

$(this).fadeTo(250,1)<br />

.find('h2').css('color','#F30').end()<br />

.find('p').('backgroundColor','#F343FF');<br />

}); // end click<br />

Notice the .end() after the .css(‘color’, ‘#F30); this code<br />

returns the <strong>jQuery</strong> selection back to the div so that the following<br />

.find(‘p’) will find all tags inside the div.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!