14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11 ■ Using Third-Party <strong>JavaScript</strong><br />

As you can see, the code is amazingly short, but it’s also rather complex in terms of syntax. Let’s go through the<br />

example bit by bit so that you can underst<strong>and</strong> what is going on:<br />

jqueryTest.js (excerpt)<br />

$(document).ready (<br />

function() {<br />

The $(document).ready () method is an event h<strong>and</strong>ler that calls the function provided as a parameter (in<br />

this case, an anonymous function) when the document is ready to be manipulated. This means that everything that<br />

follows in this script is executed when the document has loaded—<strong>and</strong> that means only the document <strong>and</strong> not all the<br />

embedded assets in it (like images). As you might remember, we talked in Chapter 5 about the ugly effect of the page<br />

content showing up before you can hide it. This method works around that problem.<br />

jqueryTest.js (continued)<br />

$('pre').before('Show code');<br />

$('pre').hide();<br />

You take every PRE element in the document <strong>and</strong> use the before() method to add a string of HTML in the <strong>DOM</strong><br />

tree before this element—in this case, a paragraph <strong>with</strong> an embedded link <strong>with</strong> the class trigger. You use the hide()<br />

method of jQuery to hide all PRE elements. (hide() sets the CSS attribute display to none .)<br />

jqueryTest.js (continued)<br />

$('a.trigger').toggle (<br />

You use the CSS selector a.trigger to match all links <strong>with</strong> the class trigger (which should be only the ones<br />

the script added via the before() method) <strong>and</strong> use the toggle() method. This method alternately executes the two<br />

functions provided as parameters when the user clicks the element. The first parameter is an anonymous function<br />

that shows the previously hidden code example <strong>and</strong> changes the link text to “Hide Code” <strong>and</strong> vice versa.<br />

jqueryTest.js (continued)<br />

function() {<br />

$(this.parentNode.nextSibling).slideDown('slow');<br />

$(this).html('Hide Code');<br />

},<br />

You can use several jQuery methods to show <strong>and</strong> hide elements, the most basic being show() <strong>and</strong> hide(). A more<br />

advanced effect is produced using slideDown() <strong>and</strong> slideUp(), which show the element in a line-by-line animation.<br />

Both of these methods take a parameter that indicates the speed of the animation, which can be slow, normal, or fast.<br />

To reach the PRE element to show or hide, you need to use the $(this) construct, which returns the event target of<br />

toggle(). This means you can use this.parentNode.nextSibling to reach the PRE, because the links are nested in<br />

a paragraph. You can change the content of the link itself via $(this) <strong>and</strong> the html() method, which takes a string of<br />

HTML as the sole parameter <strong>and</strong> changes the element’s innerHTML property.<br />

www.it-ebooks.info<br />

325

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

Saved successfully!

Ooh no, something went wrong!