Why document.ready is used in jQuery

后端 未结 4 539
忘掉有多难
忘掉有多难 2020-12-20 04:37

What is the exact use of $(document).ready() in jQuery and can we have two $(document).ready() in a webpage?

4条回答
  •  臣服心动
    2020-12-20 04:58

    Everything inside the $(document).ready() function will load as soon as the DOM is loaded and before the page contents are loaded.

    You should wrap all your javascript code with this function to ensure that the code only runs when the page is fully rendered. Without it you could run into errors where the JavaScript can't find elements as they haven't rendered to the page yet.

    And yes you can use it any number of times in a webpage.

提交回复
热议问题