jQuery in Action

104
CHAPTER 4
Events are where it happens!
originally established as a listener, it's either defined as a top-level function (so that
it can be referred to by its top-level variable name) or a reference to it is retained by
some other means. Supplying the function as an anonymous inline reference
would make it impossible to later reference the function in a call to
unbind()
.
So far, we've seen that the jQuery Event Model makes it easy to establish (as
well as remove) event handlers without worries about browser differences, but
what about writing the event handlers themselves?
4.2.3 Inspecting the Event instance
When an event handler established with the
bind()
command is invoked, the
Event
instance is passed to it as the first parameter to the function. This eliminates
the need to worry about the
window.event
property under Internet Explorer, but
what about accessing the divergent properties of the
Event
instance?
Even when using jQuery to establish handlers, the
Event
instance passed to
the event handler is a clone of the native object as defined by the browser. That
means that in standards-compliant browsers, the
Event
instance will follow the
standardized layout of properties, and under Internet Explorer, the instance will
use the proprietary layout. Before the proprietary instance is passed to the event
handler, jQuery does its best to fix up the object so that the most commonly
accessed properties and methods of that object follow the standardized format.
So once again, except for the most obscure of
Event
properties, we can write the
code for our event handlers without regard for browser platform.
Table 4.1 shows the
Event
properties that are safe to access in a platform-
independent manner.
Table 4.1 Safe
Event
instance properties
Property
Description
altKey
Set to true if the Alt key was pressed when the event was triggered, false if not.
The Alt key is labeled Option on most Mac keyboards.
ctrlKey
Set to true if the Ctrl key was pressed when the event was triggered, false if not.
data
The value, if any, passed as the second parameter to the bind() command when the
handler was established.
keyCode
For keyup and keydown events, this returns the key that was pressed. Note that for
alphabetic characters, the uppercase version of the letter will be returned, regardless
of whether the user typed an uppercase or lowercase letter. For example, both a and A
will return 65. You can use shiftKey to determine which case was entered. For key-
press events, use the which property, which is reliable across browsers.
continued on next page


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.