Why wrap code into 'document ready'

前端 未结 3 2035
情深已故
情深已故 2021-01-22 08:08

I\'m new to Jquery. See an example at http://w3schools.com/jquery/tryit.asp?filename=tryjquery_hide_p. Here, clicking \'Click Me\' button the text change. My question is why the

3条回答
  •  情书的邮戳
    2021-01-22 08:40

    Yes, if you didn't wrap this code in the $(document).ready() handler, the objects wouldn't exist because the DOM isn't loaded.

    This is a simple test to ensure that the DOM is ready, before invoking methods on its elements.

提交回复
热议问题