home / experts / javascript / column4 |
|
You've already seen how to change a link's color and text when the user points at it. As a matter of fact, it's very simple -- you embed a new tag in it. But doing this for each and every link in a document, could be very annoying, and could also increase the document's size -- and its download time as a result. Fortunately, Internet Explorer 4.0 (and Netscape Navigator 4.0x in a similar fashion) features event bubbling, which enables you to capture events that occur anywhere in the document. Even though this column does not discuss event capturing in general, we will show you the basics so you can globalize the "color change" effect. Take a look at the following JavaScript statement:
This statement assigns the function named
In order to identify the links that are playing this game, we'll assign each one a specific class. Here are the general class definitions for this example:
An explicit style definition is convenient in this case, because it enables you to add other style definitions to the link's default state and its "highlighted" state. Thus, if you want a link to play this game (to change its color, for example) you should use the
Regardless of the user's browser, the link should appear normal at this stage. Here's the preceding link "in action": Note that you should not use any of these classes for any element other than a link that should participate in the game. Take a look now at the entire script that defines the event-handling functions and assigns them the corresponding events:
If the event's source element has an It is also very important to understand why the two global statements in the script do not generate an error on older browsers, which do not support the
Now that you know why this script is compatible with all browsers despite the fact that it currently only influences Internet Explorer 4.0, it's time to take a look at a few links that use this technique. Here they are: Webreference If you're using Internet Explorer 4.0, each link turns red when you pass the mouse over it. The exact syntax for this set of links is as follows:
|
Created: October 9, 1997
Revised: December 4, 1997
URL: http://www.webreference.com/js/column4/page.html