정리/Javascript

jQuery - $(document).ready()란?

알렉스 페레이라 2023. 9. 21. 16:34

jQuery에서 제공하는 $(document).ready()는 js개발을 할때 매우 유용하게 사용되곤 하는데, DOM(Document Object Model)이 완전히 불러와지면 실행되는 Event이다. 일반적으로 브라우저가 HTML을 보여주기 위해서는 먼저 문서 구조를 만들고 만들어진 문서 구조 위에 디자인을 입히는 형식을 취하는데, 이 과정에서 디자인이 입혀지지 않은 상태로 문서 구조가 만들어진 시점에 실행되는 Event가 바로 ready().

 

사용법

$(document).ready(function(){
  // 실행할 기능을 정의해주세요.
});

참고

 jQuery 3.0 버전 이후부터는 첫 번째에 해당하는 구문만 사용하는 것을 권장드립니다. .ready() Event는 1.8 버전에서는 deprecated 되었으며 3.0에서는 지원하지 않기 때문입니다.

$(documet).ready() 대신 $()를 사용하자

$(function(){
  // 실행할 기능을 정의해주세요.
});

 

참고문서: https://learn.jquery.com/using-jquery-core/document-ready/

 

$( document ).ready() | jQuery Learning Center

A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Co

learn.jquery.com