jQuery in Action

166
CHAPTER 6
jQuery utility functions
<script type="text/javascript">
var $ = 'Hi!';
jQuery(function(){
alert('$ = '+ $);
});
</script>
</head>
<body>
</body>
</html>
In this document, we import jQuery, which (as we know) defines the global names
jQuery
and its alias
$
. We then redefine the global
$
name to a string value
b
,
overriding the jQuery definition. We replace
$
with a simple string value for sim-
plicity within this example, but it could be redefined by including another library
such as Prototype.
We then define the ready handler
c
whose only action is to display an alert
showing the value of
$
.
When we load this page, we see the alert displayed, as shown in figure 6.3.
Note that, within the ready handler, the global value of
$
is in scope and has the
expected value resulting from our string assignment. How disappointing if we
only wanted to use the jQuery definition of
$
within the handler.
Now let's make one change to this example document. Listing 6.3 shows only
the portion of the document that has been modified; the minimal change is high-
lighted in bold.
Overrides $ name
with custom value
b
Declares the
ready handler
c
Figure 6.3 The
$
says, "Hi!" as its redefinition takes effect within the ready handler.


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

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