Мне так часто бывает нужна эта информация, что время от времени забываю, как правильно написать, и каждый раз хожу на http://api.jquery.com/ready/.
Решил разместить еще одну памятку у себя в журнале.
The .ready() method can only be called on a jQuery object matching the current document, so the selector can be omitted.
The .ready() method is typically used with an anonymous function:
$(document).ready(function() {
// Handler for .ready() called.
});
Which is equivalent to calling:
$(function() {
// Handler for .ready() called.
});
If .ready() is called after the DOM has been initialized, the new handler passed in will be executed immediately.
Aliasing the jQuery Namespace
When using another JavaScript library, we may wish to call $.noConflict() to avoid namespace difficulties. When this function is called, the $ shortcut is no longer available, forcing us to write jQuery each time we would normally write $. However, the handler passed to the .ready() method can take an argument, which is passed the global jQuery object. This means we can rename the object within the context of our .ready() handler without affecting other code:
jQuery(document).ready(function($) {
// Code using $ as usual goes here.
});
Комментарии
Эмоции очень хорошо отображены.