Let's look at a simple example where mouse events are bound to the keys of a virtual music keyboard. Code Listing 4-1 defines a simple page that contains the original document structure.
Code Listings 4-1 musical.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<title>Keyboard</title>
<link rel='stylesheet' type='text/css' href='musical.css'/>
<script type='text/javascript' src='musical.js'></script>
<script type='text/javascript'>
window.onload=assignKeys;
</script>
<body>
We have declared the page to conform to strictly defined XHTML, just to show that it can be done. We assign a unique ID to the keyboard element that represents the keyboard, but we do not assign an ID to the element that represents the key. Note that each of the specified keys has two styles. Musicalbutton is common to all keys, and another individual style distinguishes them by note. These styles are defined separately in the style sheet (code listing 4-2).