Java Script &jquery PDF
Java Script &jquery PDF
Java Script &jquery PDF
murachs
JavaScript
and jQuery
(Chapter 7)
Thanks for downloading this chapter from Murachs JavaScript and jQuery. We hope
it will show you how easy it is to learn from any Murach book, with its paired-pages
presentation, its how-to headings, its practical coding examples, and its clear, concise
style.
To view the full table of contents for this book, you can go to our web site. From there,
you can read more about this book, you can find out about any additional downloads that
are available, and you can review our other books on related topics.
Thanks for your interest in our books!
After reading several other PHP books, I still was not able to get my webpage
to work the way I wanted. After reading Murachs book, it works perfectly.
Harold E. Luse; posted at an online bookseller
If you want to get up to speed with CSS and HTML, this is a must-read in my
opinion. I really liked how it gave lots of examples and had exercises at the end
of each chapter.
Posted at an online bookseller
Teaching the #html class this week. The @MurachBooks book is such
awesome courseware.
@staticvoidmain
Contents
Introduction
iii
3
49
91
119
141
157
195
231
259
287
313
343
379
405
439
469
497
535
Reference Aids
Appendix A
Appendix B
Appendix C
Index
571
581
585
588
Section 2
jQuery essentials
Now that you have the JavaScript skills that you need for using jQuery,
youre ready to learn jQuery. So, in chapter 7, youll learn a working subset
of jQuery that will get you off to a fast start. And in chapter 8, youll learn
how to use the jQuery effects and animations that can bring a web page to
life.
Then, in chapter 9, youll learn how to use the DOM manipulation and
traversal methods for advanced DOM scripting. In chapter 10, youll learn
how to use the jQuery features for working with forms. And in chapter 11,
youll learn how to use the many jQuery plugins that can quickly improve
your productivity, and youll also learn how to create your own plugins.
When you complete this section, youll have all the jQuery skills that
you need for developing professional web pages. You can also go on to any
of the three sections that follow because they are written as independent
modules. If, for example, you want to learn how to use Ajax next, skip to
section 4.
7
Get off to a fast start
with jQuery
In this chapter, youll quickly see how jQuery makes JavaScript programming
easier. Then, youll learn a working subset of jQuery that will get you off to a
fast start. Along the way, youll study four complete applications that will show
you how to apply jQuery.
Introduction to jQuery........................................................196
What jQuery is..............................................................................................196
How to include jQuery in your web pages...................................................196
How jQuery can simplify JavaScript development......................................198
How jQuery can affect testing and debugging.............................................198
How jQuery UI and plugins can simplify JavaScript development............ 200
Perspective..........................................................................228
196
Section 2
jQuery essentials
Introduction to jQuery
In this introduction, youll learn what jQuery is, how to include it in your
applications, and how jQuery, jQuery UI, and plugins can simplify JavaScript
development.
What jQuery is
As figure 7-1 summarizes, jQuery is a free, open-source, JavaScript library
that provides dozens of methods for common web features that make JavaScript
programming easier. Beyond that, the jQuery functions are coded and tested for
cross-browser compatibility, so they will work in all browsers.
Those are two of the reasons why jQuery is used by more than half of the
10,000 most-visited web sites today. And thats why jQuery is commonly used by
professional web developers. In fact, you can think of jQuery as one of the four
technologies that every web developer should know how to use: HTML, CSS,
JavaScript, and jQuery. But dont forget that jQuery is actually JavaScript.
Chapter 7
How to include the jQuery file from a Content Delivery Network (CDN)
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
Description
jQuery is a free, open-source, JavaScript library that provides methods that make
JavaScript programming easier. Today, jQuery is used by more than 50% of the
10,000 most-visited web sites, and its popularity is growing rapidly.
The jQuery download comes in two versions. One version (min) is a compressed
version that is relatively small and loads fast. The other version is uncompressed so
you can use it to study the JavaScript code in the library.
If you include the jQuery file from a Content Delivery Network (CDN), you dont
have to provide it from your own server, but then you cant work offline.
The jQuery CDN now provides a link that will always deliver the latest version of
jQuery. Thats the way the script element is coded in all of the applications in this book.
If you download the jQuery file to your system, you can change the filename so its
simpler, but then you may lose track of what version youre using.
Figure 7-1
197
198
Section 2
jQuery essentials
which selects all of the h2 elements in the element with faqs as its id. In fact,
jQuery supports all of the CSS selectors including the CSS3 selectors, even in
browsers that dont support all of the CSS3 selectors. This is another reason why
developers like jQuery.
Chapter 7
Figure 7-2
199
200
Section 2
jQuery essentials
Chapter 7
Description
jQuery UI is a free, open-source, JavaScript library that provides higher-level
effects, widgets, and mouse interactions that can be customized by using themes. In
section 3, youll learn how to use jQuery UI.
A plugin is a JavaScript library that provides functions that work in conjunction
with jQuery to make it easier to add features to your web applications. In chapter
11, youll learn how to use some of the most useful plugins, and youll also learn
how to create your own plugins.
In general, if you can find a plugin or jQuery UI feature that does what you want it
to do, you should use it. Often, though, you wont be able to find what you want so
youll need to develop the feature with just the core jQuery library.
Figure 7-3
201
202
Section 2
jQuery essentials
Chapter 7
The HTML for the elements that are selected by the examples
<section id="faqs">
<h1>jQuery FAQs</h1>
<h2 class="plus">What is jQuery?</h2>
<div>
<p>jQuery is a library of the JavaScript functions that you're most
likely to need as you develop web sites.
</p>
</div>
<h2 class="plus">Why is jQuery becoming so popular?</h2>
<div>
<p>Three reasons:</p>
<ul>
<li>It's free.</li>
<li>It lets you get more done in less time.</li>
<li>All of its functions cross-browser compatible.</li>
</ul>
</div>
</section>
Adjacent siblings: All div elements that are adjacent siblings of h2 elements
$("h2 + div")
Description
When you use jQuery, the dollar sign ($) is used to refer to the jQuery library.
Then, you can code selectors by using the CSS syntax within quotation marks
within parentheses.
Figure 7-4
203
204
Section 2
jQuery essentials
Chapter 7
val(value)
text()
text(value)
next([type])
Get the next sibling of an element or the next sibling of a specified type if the parameter is coded.
submit()
focus()
Examples
How to get the value from a text box
var gallons = $("#gallons").val();
How to set the text for the next sibling with object chaining
$("#last_name").next().text("Last name is required");
Description
To call a jQuery method, you code a selector, the dot operator, the method name,
and any parameters within parentheses. Then, that method is applied to the element
or elements that are selected by the selector.
When you use object chaining with jQuery, you code one method after the other.
This works because each method returns the appropriate object.
If the selector for a method selects more than one element, jQuery applies the
method to all of the elements so you dont have to code a loop to do that.
Figure 7-5
205
206
Section 2
jQuery essentials
Chapter 7
Description
ready(handler)
click(handler)
Two ways to code an event handler for the jQuery ready event
The long way
$(document).ready(function() {
alert("The DOM is ready");
});
Description
To code a jQuery event handler, you code a selector, the dot operator, the name of
the jQuery event method, and an anonymous function that handles the event within
parentheses.
The event handler for the ready event will run any methods that it contains as soon
as the DOM is ready, even if the browser is loading images and other content for the
page. This works better than the JavaScript onload event, which doesnt occur until
all of the content for the page is loaded.
In this book, the ready event is always coded the long way thats shown above. In
practice, though, many programmers use the short way.
When coding one event handler within another, the use of the closing braces, parentheses, and semicolons is critical. To help get this right, many programmers code
inline comments after these punctuation marks to identify the ends of the handlers.
Figure 7-6
207
208
Section 2
jQuery essentials
Chapter 7
The HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Join Email List</title>
<link rel="stylesheet" href="email_list.css">
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
</script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="email_list.js"></script>
</head>
<body>
<section>
<h1>Please join our email list</h1>
<form id="email_form" name="email_form"
action="join.html" method="get">
<label for="email_address1">Email Address:</label>
<input type="text" id="email_address1">
<span>*</span><br>
<label for="email_address2">Re-enter Email Address:</label>
<input type="text" id="email_address2">
<span>*</span><br>
<label for="first_name">First Name:</label>
<input type="text" id="first_name">
<span>*</span><br>
<label> </label>
<input type="button" id="join_list" value="Join our List">
</form>
</section>
</body>
</html>
Figure 7-7
The user interface and HTML for the Email List application
209
210
Section 2
jQuery essentials
The jQuery
Figure 7-8 presents the jQuery for this application. This is the code in the
email_list.js file thats included by the HTML. Here, all of the jQuery is highlighted. The rest of the code is JavaScript code.
To start, you can see that an event handler for the click event of the Join our
List button is coded within the event handler for the ready event. Then, if you
look at the last three lines of code, you can see the ending punctuation marks
for these handlers. Within the click event handler, the first two statements show
how jQuery selectors and the val method can be used to get the values from text
boxes.
In the first if statement, you can see how an error message is displayed if the
user doesnt enter an email address in the first text box. Here, the next method
gets the adjacent sibling for the text box, which is the span element, and then the
text method puts an error message in that span element. This changes the DOM,
and as soon as it is changed, the error message is displayed in the browser.
The next, text, and val methods are used in similar ways in the next two if
statements. Then, the fourth if statement tests to see whether the isValid variable
is still true. If it is, the submit method of the form is issued, which sends the data
to the web server.
That ends the event handler for the click event of the Join our List button.
But that handler is followed by one more statement. It moves the focus to the first
text box, the one with email_address1 as its id.
As you review this code, note that it doesnt require the standard $ function
that gets an element object when the elements id is passed to it. Instead, the $
sign is used to start a jQuery selector that gets elements by their ids. This simplifies the coding.
Although this jQuery code illustrates how jQuery can simplify a data validation application, it doesnt begin to show the power of jQuery. For that, you need
to learn more selectors, methods, and event methods, and then see how they can
be used in other types of applications. Youll do that next.
Chapter 7
Figure 7-8
211
212
Section 2
jQuery essentials
This just shows that you can often select the elements that you want in more than
one way.
Chapter 7
[attribute=value]
:contains(text)
:empty
:eq(n)
:even
:first
:first-child
:gt(n)
All elements within the selected set that have an index greater than n.
:has(selector)
:header
:hidden
:last
:last-child
All elements that are the last children of their parent elements.
:lt(n)
All elements within the selected set that have an index less than n.
:not(selector)
:nth-child
All elements that are the nth children of their parent elements.
:odd
:only-child
All elements that are the only children of their parent elements.
:parent
All elements that are parents of other elements, including text nodes.
:text
:visible
Examples
How to select the li elements that are the first child of their parent element
$("li:first-child")
// numbering starts at 0
How to select all input elements with text as the type attribute
$(":text")
Description
Figure 7-4 and the table above summarize the selectors that you are most likely to need.
Not included are six attribute selectors that let you select attributes with attribute
values that contain specific substrings.
In chapter 8, youll learn about a selector thats used with animation, and in chapter
10, youll learn about other selectors that are used for form controls.
Figure 7-9
213
214
Section 2
jQuery essentials
Chapter 7
prev([selector])
attr(attributeName)
attr(attributeName, value)
css(propertyName)
css(propertyName, value)
addClass(className)
Add one or more classes to an element and, if necessary, create the class. If you use more than one class as the parameter,
separate them with spaces.
removeClass([className])
Remove one or more classes. If you use more than one class
as the parameter, separate them with spaces.
toggleClass(className)
hide([duration])
show([duration])
each(function)
Set the value of the src attribute of an image to the value of a variable
$("#image").attr("src", imageSource);
Description
The table above presents a powerful set of jQuery methods. This table adds to the
methods that were presented in figure 7-5.
In the chapters that follow, youll learn how to use other methods, including those
for effects (chapter 8), DOM scripting and traversal (chapter 9), forms (chapter 10),
and Ajax and JSON (chapter 14).
Figure 7-10
215
216
Section 2
jQuery essentials
Chapter 7
Description
ready(handler)
unload(handler)
error(handler)
click(handler)
toggle(handlerEven, handlerOdd)
dblclick(handler)
mouseenter(handler)
mouseover(handler)
mouseout(handler)
hover(handlerIn, handlerOut)
A handler for the hover event of each img element within a list
$("#image_list img").hover(
function() {
alert("The mouse pointer has moved into an img element");
},
function() {
alert("The mouse pointer has moved out of an img element);
}
); // end hover
Description
The table above presents the event methods that youll use the most. Not included
are: keydown, keypress, keyup, mousedown, mouseup, mouseleave, and
mousemove.
Figure 7-11
217
218
Section 2
jQuery essentials
Chapter 7
Description
bind(event, handler)
unbind(event, handler)
one(event, handler)
trigger(event)
How to use the shortcut method to trigger an event from an event handler
$(":text").dblclick(function () {
$("#clear").click();
// triggers the click event of the clear button
}
Description
When you store an event handler in a variable, you can use the bind method to
attach it to more than one event.
When you use the shortcut method to attach an event handler to an event, youre
actually using the bind method.
The submit and focus methods are actually event methods. When you use the shortcut method to trigger them, they trigger the submit and focus events.
Figure 7-12
219
220
Section 2
jQuery essentials
Remember that the parameters for the toggle event method are two functions, separated by a comma. So thats whats coded within the parentheses for
this method. In both of these functions, the this keyword is used, and it refers to
the current h2 element because thats the object of the toggle method.
In the first function (parameter) for this method, the first statement uses the
addClass method to add a class attribute to the h2 element with minus as the
value. The CSS for this application (not shown) uses this class to change the
background image for the element from a plus sign to a minus sign. Then, the
second statement chains the next and show methods to display the hidden div
element that is an adjacent sibling to the h2 element.
The second function for this method works the same way, but in reverse.
There, the first statement uses the removeClass method to remove the minus
class that was added the first time the heading was clicked. The second statement
uses the next and hide methods to hide the adjacent div element.
This application begins to show the power of jQuery. Here, it takes just 12
lines of code to set up the event handlers for alternating clicks of as many h2 elements as there are in the faqs section.
If you compare this code with the JavaScript code for this application in
figure 7-2, you can get a better idea of how much this simplifies this application.
In fact, since JavaScript doesnt provide for show and hide methods, the
JavaScript has to use a class and CSS to hide and close the div elements.
Chapter 7
The HTML
<section id="faqs">
<h1>jQuery FAQs</h1>
<h2>What is jQuery?</h2>
<div>
<p>jQuery is a library of the JavaScript functions that you're most
likely to need as you develop web sites.
</p>
</div>
<h2>Why is jQuery becoming so popular?</h2>
<div>
<p>Three reasons:</p>
<ul>
<li>It's free.</li>
<li>It lets you get more done in less time.</li>
<li>All of its functions are cross-browser compatible.</li>
</ul>
</div>
<h2>Which is harder to learn: jQuery or JavaScript?</h2>
<div>
<p>For most functions, jQuery is significantly easier to learn and
use than JavaScript. But remember: jQuery is JavaScript.
</p>
</div>
</section>
The jQuery
$(document).ready(function() {
$("#faqs h2").toggle(
function() {
$(this).addClass("minus");
$(this).next().show();
},
function() {
$(this).removeClass("minus");
$(this).next().hide();
}
); // end toggle
}); // end ready
Figure 7-13
221
222
Section 2
jQuery essentials
Chapter 7
The HTML
<section>
<h1>Ram Tap Combined Test</h1>
<ul id="image_list">
<li><a href="images/h1.jpg" title="James Allison:
<img src="thumbnails/t1.jpg" alt=""></a></li>
<li><a href="images/h2.jpg" title="James Allison:
<img src="thumbnails/t2.jpg" alt=""></a></li>
<li><a href="images/h3.jpg" title="James Allison:
<img src="thumbnails/t3.jpg" alt=""></a></li>
<li><a href="images/h4.jpg" title="James Allison:
<img src="thumbnails/t4.jpg" alt=""></a></li>
<li><a href="images/h5.jpg" title="James Allison:
<img src="thumbnails/t5.jpg" alt=""></a></li>
<li><a href="images/h6.jpg" title="James Allison:
<img src="thumbnails/t6.jpg" alt=""></a></li>
</ul>
<h2 id="caption">James Allison 1-1</h2>
<p><img src="images/h1.jpg" alt="" id="image"></p>
</section>
1-1">
1-2">
1-3">
1-4">
1-5">
1-6">
Figure 7-14
The user interface, HTML, and CSS for the Image Swap application
223
224
Section 2
jQuery essentials
Figure 7-15 presents the JavaScript and jQuery for this application. Within
the ready event handler, the each method is used to run a function for each <a>
element in the unordered list. This function preloads the images that will be
swapped so they are in the browser when the user starts using the application. If
an application uses many images, this can make the application run faster.
Within the function for each <a> element, the first statement creates a new
Image object. Then, the second statement uses the this keyword and the attr
method to set the src attribute of the Image object to the value of the href attribute in the <a> element. As soon as thats done, the image is loaded into the
browser.
The each method is followed by a click event method that sets up the event
handlers for the click events of the <a> elements that contain the thumbnail
images. Note here that evt is coded as the parameter for the click event method,
which receives the event object when the event occurs. This object will be used
later to cancel the default action of each link.
The first two statements in this event handler swap the image in the main
portion of the browser window. The first statement uses the this keyword and attr
method to get the value of the href attribute of the <a> element. This value gives
the location of the image to be swapped. Then, the second statement sets the src
attribute of the main img element to this value. As soon as thats done, the image
is swapped in the browser.
The next two statements work similarly. They swap the caption of the image
in the main portion of the browser window. This time, the caption is taken from
the title attribute of the <a> element.
The last statement in the click event handler uses the preventDefault method
of the evt object that is passed to the event handler to cancel the default action of
the link. This is a jQuery method that is cross-browser compatible, which simplifies the JavaScript code. It is executed in place of the preventDefault method of
the browser when you include the jQuery library. If the default isnt cancelled,
clicking on the link will display the image thats referred to by the href attribute
in a new window or tab, and that isnt what you want.
After the click event handler, the last statement in the ready method moves
the focus to the first thumbnail image. That will make it easier for the users to
tab to the thumbnails that they want to swap. To identify the first <a> element in
the thumbnails, the :first-child selector is used twice: first to get the first li element of the unordered list, then to get the first <a> element of the li element.
Chapter 7
The JavaScript
$(document).ready(function() {
// preload images
$("#image_list a").each(function() {
var swappedImage = new Image();
swappedImage.src = $(this).attr("href");
});
// set up event handlers for links
$("#image_list a").click(function(evt) {
// swap image
var imageURL = $(this).attr("href");
$("#image").attr("src", imageURL);
//swap caption
var caption = $(this).attr("title");
$("#caption").text(caption);
// cancel the default action of the link
evt.preventDefault(); // jQuery cross-browser method
}); // end click
// move focus to first thumbnail
$("li:first-child a:first-child").focus();
}); // end ready
Description
When you attach an event handler to a link, you often need to cancel the default
action of the link, which is to open the page or image thats identified by its href
attribute.
To cancel the default action of a link, you can use the jQuery preventDefault
method of the event object that is passed to the event handler. The jQuery version of
this method is cross-browser compatible.
To get the event object thats passed to a method, you need to code a parameter for
the event handler function. You can use whatever name you want for this parameter,
like evt or event. Then, you must use that name whenever you refer to the event
object.
When you use jQuery to work with images that arent included in the HTML,
preloading the images can improve the performance of the application because the
user doesnt have to wait for a new image to be loaded into the browser.
To preload an image, you create a new Image object and assign the URL for the
image to the Image objects src attribute. As soon as thats done, the image is
loaded into the browser.
Figure 7-15
225
226
Section 2
jQuery essentials
The function for the each method starts by using the this keyword and
the attr method to get the values of the src and id attributes of each image and
storing them in variables named oldURL and newURL. Remember that these
attributes hold the values that locate each starting image and its rollover image.
If you look at the HTML, you can see that these images are jpg files that are
stored in an images folder. For instance, the rollover image specified by the first
id attribute is:
images/h4.jpg
The next two statements preload the rollover images by creating a new
Image object for each one and assigning the value of the newURL variable to
its src attribute. As soon as the src attribute is set, the browser loads the image.
Although preloading isnt essential, it can improve the user experience because
the users wont have to wait for a rollover image to be loaded.
At this point, the hover event method is used to set up the event handlers
for each image. Remember that this event method has two event handlers as its
parameters. The first event handler is run when the mouse pointer moves into
the element, and the second one is run when the mouse pointer moves out of the
image. Here, the this keyword is used so the hover event applies to each img
element.
In the function for the first event handler, you can see that the this keyword
and the attr method are used to set the src attribute of the image to the value
of the newURL variable. That causes the rollover image to be displayed. The
function for the second event handler reverses this process by restoring the src
attribute of the image to the value of the oldURL variable.
Of course, theres more than one way to code an application like this. For
instance, you could implement this application by using the mouseover and
mouseout event methods instead of the hover event method. Then, you would
code the first function of the hover event as the mouseover event handler and the
second function as the mouseout event handler.
Chapter 7
The HTML
<section>
<h1>Ram Tap Combined Test</h1>
<ul id="image_rollovers">
<li><img src="images/h1.jpg" alt="" id="images/h4.jpg"></li>
<li><img src="images/h2.jpg" alt="" id="images/h5.jpg"></li>
<li><img src="images/h3.jpg" alt="" id="images/h6.jpg"></li>
</ul>
</section>
The JavaScript
$(document).ready(function() {
$("#image_rollovers img").each(function() {
var oldURL = $(this).attr("src");
// gets the src attribute
var newURL = $(this).attr("id");
// gets the id attribute
// preload images
var rolloverImage = new Image();
rolloverImage.src = newURL;
// set up event handlers
$(this).hover(
function() {
$(this).attr("src", newURL);
},
function() {
$(this).attr("src", oldURL);
}
); // end hover
}); // end each
}); // end ready
Figure 7-16
227
228
Section 2
jQuery essentials
Perspective
To get you off to a fast start, this chapter has presented a working subset of
the most useful jQuery selectors, methods, and event methods. That should give
you some idea of what you have to work with when you use jQuery. And youll
add to those selectors and methods as you read other chapters in this book.
The trick of course is being able to apply the right selectors, methods, and
event methods to the application that youre trying to develop. To get good at
that, it helps to review many different types of jQuery applications. Thats why
this chapter has presented four applications, and thats why youll see many
more before you complete this book.
Terms
jQuery method
CDN (Content Delivery Network)
getter method
jQuery UI
setter method
plugin
object chaining
selector
event method
Summary
jQuery is a JavaScript library that provides methods that make JavaScript
programming easier. These methods have been tested for cross-browser
compatibility.
jQuery UI and plugins are JavaScript libraries that use the jQuery library to
build higher-level features.
To use jQuery, you code a script element in the head section that includes
the file for the jQuery core library. This file can be downloaded and stored
on your computer or server, or you can access it through a Content Delivery
Network (CDN).
When you code statements that use jQuery, you use selectors that are like
those for CSS. You also use a dot syntax that consists of the selector for
one or more elements, the dot, and the name of the method that should be
executed.
To set up event handlers in jQuery, you use event methods. Most of these
methods have one parameter that is the event handler that will be run when
the event occurs. But some event methods like the toggle method take two
event handler parameters.
Exercise 7-1
Chapter 7
In this exercise, youll add a Clear button to the Email List application of figures
7-7 and 7-8. That will force you to create and attach another event handler.
1. Use your text editor to open the index.html and email_list.js files in this
folder:
c:\jquery\exercises\ch07\email_list\
2. Run the application to refresh your memory about how it works. Note that a
Clear button has been added below the Join our List button, but the button
doesnt work.
3. Add an event handler for the click event of the Clear button that clears all of
the text boxes by setting them to an empty string (). This can be done in one
statement. To select just the text boxes, you use a selector like the last one in
figure 7-9. To set the values to empty strings, you use the val method like the
second example in figure 7-5. Now, test this change.
4. This event handler should also put the asterisks back in the span elements that
are displayed to the right of the text boxes to show that entries are required.
That requires just one statement that uses the next and the text methods.
5. Add one more statement to this event handler that moves the focus to the first
text box. Then, test this change.
6. Add another event handler to this application for the double-click event of any
text box. This event handler should do the same thing that the event handler
for the click event of the Clear button does. The easiest way to do that is to
trigger the click event of the Clear button from the handler for the double-click
event, as in the last example in figure 7-12.
7. Comment out the line of code that you just used to trigger the click event of
the Clear button. Then, add a statement to the double-click event handler that
only clears the text from the text box that the user double-clicks in. To do that,
youll have to use the this keyword, as in the first example in figure 7-11.
Exercise 7-2
This exercise asks you to modify the Image Rollover application of figure 7-16
so it uses different events for the event handlers.
1. Use your text editor to open the HTML and JavaScript files that are in this
folder:
c:\jquery\exercises\ch07\image_rollover\
229
230
Section 2
jQuery essentials
3. Comment out the hover method in the JavaScript, and rewrite the code
so it uses the mouseover and mouseout event methods to implement this
application. That should take about five minutes.
Exercise 7-3
In this exercise, youll start with the HTML and CSS for the user interface that
follows. Then, youll develop the jQuery code that makes it work.
Development guidelines
1. Youll find the HTML, CSS, and image files for this application in this folder:
c:\jquery\exercises\ch07\book_list\
Youll also find an empty JavaScript file named book_list.js. You can add your
code to this file.
2. This application works like the FAQs application you saw in this chapter,
except that a list of book links is displayed below each heading. If the user
clicks on one of these links, an image for the book is displayed to the right of
the list. In addition, anytime the user clicks on a heading with a plus or minus
sign before it, the image should no longer be displayed.
3. The HTML for the links of this application is like the HTML for the Image
Swap application. However, the links for this application dont require the title
attribute since no caption is displayed for the image.
4. The images that are referred to by the href attributes of the links in this
application should be preloaded. To do that, you can loop through all the links
in the section element. Also, be sure to cancel the default actions of the links.
5. Feel free to copy and paste code from any of the applications that are available
to you. Thats the most efficient way to build a new application.
IE note: If you set the src attribute of the img element to an empty string so
no image is displayed when the application starts and when the user clicks on
a heading, a placeholder will be displayed for this element in Internet Explorer.
To avoid that, you can set the style attribute of the img element so the display
property is set to none. Then, you can set this property to block when you
want to display an image. In chapter 9, youll learn an easier way to set CSS
properties using jQuery.
Look up coding details or refresh your memory on forgotten details when youre in
the middle of developing a web application
Loan to your colleagues who are always asking you questions about client-side
web programming
100% Guarantee
You must be satisfied. Each book you buy directly
from us must outperform any competing book or
course youve ever tried, or send it back within 90
days for a full refundno questions asked.
Thanks for your interest in Murach books!