To expand on this just a bit, Shelane used what is perhaps a cryptic syntax here.

    $(function() {
        // ...
    });

could also be written

    $(document).ready(function() {
        // ...
    });

So the code inside this function will now be run when the document is ready, i.e. when the DOM is loaded. The original had the following code being run as the HEAD is being loaded:

    $("a").click(function(){
        alert("ahh");
        return false;
    });

This code binds a function to the click event of each <a> element found in the DOM. But since the browser hasn't started parsing the DOM for the body yet, there are no <a> elements to bind to, so nothing happens.

If you wrap it up in the $(document).ready() call, this should work fine.

Cheers,

  -- Scott


Shelane wrote:
You tried wrapping it in a ready function?
<script type="text/javascript">
$(function(){
$("a").click(function(){
   alert("ahh");
   return false;
 });
});
    </script>


On Jul 4, 12:10 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
I have the following code in my file, the src for the jquery file is
correct. I view the page in the browser and it just goes to the jQuery
page, no alert box. Any idea why this is happening?

<html>
  <head>
    <script type="text/javascript" src="jquery-1.1.3.js"></script>
    <script type="text/javascript">
$("a").click(function(){
   alert("ahh");
   return false;
 });
    </script>
  </head>
  <body>
    <a href="http://jquery.com/";>jQuery</a>
  </body>
  </html>





Reply via email to