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.

JavaScript Events and jQuery 299<br />

<br />

Your second JavaScript page.<br />

<br />

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

$("#contentDiv").click(function() {<br />

$(this).fadeOut(5000);<br />

});<br />

<br />

<br />

<br />

Note that the .click() function is attached directly to the that has the id of content-<br />

Div. The .click() function itself calls another function (an anonymous function, enclosed<br />

in curly brackets) which calls the .fadeOut() function. You’ll notice that there’s a new part<br />

here, the $(this) identifier. The $(this) identifier refers to the item that raised the event,<br />

so in the example shown, $(this) refers to the contentDiv element. You could also write it<br />

like this:<br />

$("#contentDiv").click(function() {<br />

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

});<br />

When you load this page in a Web browser you’ll see a screen like the one shown<br />

below. When you click within the element, the entire will slowly fade out.

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

Saved successfully!

Ooh no, something went wrong!