Web Development with jQuery®

(Elliott) #1

Enumerating Arrays (^) ❘ 141
array, you’ll fi nd that the expression (variable instanceof Array) evaluates to true. In this case, the
object is not an instanceof Array, so the expression evaluates to false, and the text "Albums is a
plain object." is written to the JavaScript console.
NOTE Emulating the functionality of the break and continue keywords using
jQuery’s each() method is handled in a very intuitive way. All you have to do is
write a return statement in your callback function. Returning false from your
callback function stops iteration, just like using a break keyword in a normal
loop, and returning true continues iteration to the next item immediately, just
like using a continue keyword.


Iterating a Selection


jQuery’s each() method doesn’t have to be applied to an array or object, however; it can also be
applied to a selection of elements. The following document, Example 5-3, demonstrates how each()
can be used to iterate over a selection of elements:

<!DOCTYPE HTML>
<html lang='en'>
<head>
<meta http-equiv='X-UA-Compatible' content='IE=Edge' />
<meta charset='utf-8' />
<title>Rubber Soul</title>
<script src='../jQuery.js'></script>
<script src='../jQueryUI.js'></script>
<script src='Example 5-3.js'></script>
<link href='Example 5-3.css' rel='stylesheet' />
</head>
<body>
<h4>Rubber Soul</h4>
<ul id='rubberSoul'>
<li>Drive My Car</li>
<li>Norwegian Wood (This Bird Has Flown)</li>
<li>You Won't See Me</li>
<li>Nowhere Man</li>
<li>Think for Yourself</li>
<li>The Word</li>
<li>Michelle</li>
<li>What Goes On</li>
<li>Girl</li>
<li>I'm Looking Through You</li>
<li>In My Life</li>
<li>Wait</li>
<li>If I Needed Someone</li>
<li>Run for Your Life</li>
</ul>
</body>
</html>

http://www.it-ebooks.info

Free download pdf