What does this jQuery code mean and when will it be called?

I am new to jQuery and I see a piece of code on a web page:

$(function() {
     //do something...
});

I do not understand the syntax here. Does it define a function? When will he be called? Can i reuse it?

thank

+3
source share
4 answers

This is a short version of the jQuerys DOM handler. The code inside the function will be called when the page loads, when the event is DOMReadyfired.

This is equivalent to the following, which is a bit more verbose but clear:

$(document).ready(function() {
    // code here
});
+4
source

The function will be called as soon as the document object model (DOM) is ready (page loading).

This is a shorthand for:

$(document).ready(function() { 
  // Commands here
});

/ , .

+2

ready(), DOM.

$(document).ready(function() {

});

, DOM ( ).

$(window).load(function() {

});

, ( )

+1

, jQuery. ( ). , , (, , ):

$(document).ready(function() {
  // anything inside this will happen only after the document has fully loaded
});

script, :

<script type="text/javascript">
  $('div').css('background-color','green'); // sets all <div> CSS background to green
</script>

div ( <head>, ). $(function() { }); $(document).ready(function() { }); ( , , ), div, DOM ( HTML, ).

And yes, you can use it as many times as you want on your page.

More information here: Tutorials: Presenting $ (document) .ready ()

+1
source

All Articles