开发者

Why document.ready waits?

I know that Document.ready - DONt wait for images to download.

So why it does here ?

http://jsbin.com/ehuke4/27/edit#source

(after each test - change the v=xxx in the img SRC)

   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></开发者_如何学编程script>    
  <script type="text/javascript">

        $(document).ready(function(){
        alert('0');
         });

    </script>

    </head>
      <body  >
        <img src='http://musically.com/blog/wp-content/uploads/2011/04/Google-Logo.jpg?v=42333'  />

    </body>

    </html>


I think, the problem comes out from JSBin.com

Because, when you try this example on JSFiddle.net, it works properly

http://jsfiddle.net/vqte9/


It has to do with the fact that you're using "alert()", I think, though I'm not 100% sure why. If you change your code like this:

  <body>
    <div id='x'></div>
    <img ...>
  </body>
  <script>
    $(function() { $('#x').text("hello there"); });
  </script>

you'll see that the text is filled in before the image loads.

edit — I don't know why this would make a difference, but I notice quite different behavior when I set up the ready handler with:

$(function() { whatever; });

and:

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

Now that's not supposed to be the case; the two forms are supposed to do exactly the same thing, as far as I know. However, they don't seem to. Here is a jsbin example that sets up the ready handler with the first form, and here is one that uses the second one. They behave very differently for me. I'll have to check the jQuery source to figure out how that can be true.

Here is the jQuery documentation explaining the equivalence of $(handler) and $(document).ready(handler).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜