10.07.2017 Views

246996016-HTML5-Step-by-Step

Create successful ePaper yourself

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

296 Chapter 16<br />

You can also select elements <strong>by</strong> their cascading style sheet (CSS) class with a dot prefix,<br />

similar to that used in the CSS file itself:<br />

$(".className")<br />

jQuery provides several other ways to select elements, including hierarchical functions<br />

<strong>by</strong> which you can select the children of an element; a way to select every other element<br />

except a specified element; an element’s parent elements; and many other selectors. See<br />

http://api.jquery.com/category/selectors for information on all the available selectors in<br />

jQuery.<br />

What can you do with a selected element? The answers are virtually limitless. For<br />

instance, recall that in an example in an earlier chapter you changed the background<br />

color of the to gray using JavaScript. Here’s how you would do the same thing<br />

using jQuery:<br />

$("#contentDiv").css("backgroundColor","#abacab");<br />

Here’s a bonus example using the jQuery fadeOut() function:<br />

<br />

<br />

<br />

<br />

jQuery 101<br />

<br />

<br />

<br />

Your second JavaScript page.<br />

<br />

$("#contentDiv").css("backgroundColor", "#abacab");<br />

$("#contentDiv").fadeOut();<br />

<br />

<br />

<br />

Viewing this page in a browser will result in the contentDiv fading out after a certain<br />

default (and short) time period. If you find that time period too short, you can specify the<br />

duration, as well. Time is measured in milliseconds for this (and most other) functions in<br />

jQuery, so every second is 1000 milliseconds. Therefore, to set the fade-out duration to<br />

5 seconds, you would write:<br />

$("#contentDiv").fadeOut(5000);<br />

See Also The .fadeOut() function accepts other arguments as well. See http://api.jquery.com/<br />

fadeOut/ for more information on how to use this function.

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

Saved successfully!

Ooh no, something went wrong!