program story

이 jQuery 준비 기능의 차이점은 무엇입니까?

inputbox 2020. 10. 27. 08:07
반응형

이 jQuery 준비 기능의 차이점은 무엇입니까?


차이점은 무엇입니까

$(function(){

}); 

$(document).ready(function() { 

});

아무것도 없습니다.

이 함수는 $ (document) .ready ()와 같이 작동합니다. 다른 $ ()를 래핑하는 데 사용해야한다는 점에서

소스 코드 에서 볼 수 있습니다 .

rootjQuery = jQuery(document);

...

} else if ( jQuery.isFunction( selector ) ) {
    return rootjQuery.ready( selector );
}

} else if (jQuery.isFunction(selector)) {
    return rootjQuery.ready(selector);
}

로부터 소스

호출 $(document).ready(selector)하면 몇 가지 if 문이 저장됩니다.

jQuery는 $(document)내부적으로 캐시 $(f)하므로 더 빠를 수 있습니다 .

벤치마킹


둘 다 동등하며 첫 번째는 축약 형입니다.


$ (function () {})은 DOM 준비를위한 지름길입니다.

jQuery 생성자에 인수로 전달 된 함수는 문서 준비 이벤트에 바인딩됩니다.


둘은 정확히 동일합니다. 원하는 형식을 사용하십시오.

즉, 저는 개인적으로 코드가 무엇을하는지 완전히 명백한 단순한 이유 때문에 항상 확장 된 형태 $(document).ready(function(){});사용합니다 . 대략적인 아이디어는 "자체 문서화 코드"입니다. 나중에 코드를 확인하는 사람은 코드가 documentready이벤트 에서 실행될 것임을 즉시 알 수 있습니다 . 축약 형을 사용하면 의미를 이해하는 코드 독자에게 의존해야합니다.


나는 이것을 읽는 것이 좋습니다 . 보시다시피

다음 세 가지 구문은 모두 동일합니다.

$(document).ready(handler)

$().ready(handler) (this is not recommended)

$(handler)

따라서 그것은 당신과 당신이 선호하는 것에 달려 있습니다.


그들은 사실상 동일합니다. 차이 없음.


이것이 기본 방식입니다.

$(document).ready(function() {
    // code
});

그리고 이것은 이전의 속기입니다.

$(function() {
    // code
});

jQuery 소스 코드


We have run into situations where IE9 does not run functions within $(function() {}); in the same manner or timing as $(document).ready(function(){});

The issue reared its head for us specifically in reading information out of a query string and processing and displaying that information on the screen, or using it to process a form. IE9 would process the information once it was cached with $(function(), and a user refreshed the page. But on first run, nothing worked right. However, once we switching from $(function(){}); to $(document).ready(), the issue was fixed. We changed NOTHING else.

I so look forward to the day I don't have to test for IE9 and lower.


I use $(function() {}); because it's shorter. As far as I know there is no difference between the two ways of doing it.

참고URL : https://stackoverflow.com/questions/2662778/what-is-the-difference-between-these-jquery-ready-functions

반응형