Why wrap code into 'document ready'

前端 未结 3 1982
情深已故
情深已故 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:46

    If you don't wrap it, it might not find your element. The page needs to be build first, and then you can look for certain elements to add some functions.

    So you say "wait for the page to be finished loading, so all elements I want to change are there" before you start adding stuff to them.

提交回复
热议问题