Understanding an event prepared by documents

Hey, I just wrote another basic script for my JS and jQuery practice, and I just dug up the event, ready for goold ol release, when I realized that it really is, and I wanted to know if I were right or not :

Here is a doc ready for jQuery:

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

Now the dollar sign is a shorthand for jQuery, which calls the jQuery library, so we can make jQuery instructions and calls, right?

(document) is a selector that refers to the "highest" part of the DOM (except for the window?).

.ready is an action that occurs when the DOM is fully loaded. Now that the DOM is fully loaded, does the DOM in this case refer to what is selected? So, if the body were selected instead of the document, would it execute the script before loading?

(function(){});

This part bothers me a bit.

I know that as soon as our document loads, it will run our script. In other words, will it perform our function correctly? Is our whole script considered a function? And this is really just one big JavaScript operator, right? Because it ends in the form of a colon. Also, why does our script usually go between parentheses and not parentheses of a function? What is the difference?

Thank you so much, sorry for asking n00by, I was just curious! Xd

+3
source share
7 answers

Wow, this is a question question for one answer :)

jQuery, jQuery, jQuery, ?

, $ jQuery . jQuery:

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

window - . , , , window.$ $ .

() - , " " DOM ( ?).

document , DOM, node DOM. , document.domain .. - <html>.

.ready - , , DOM . , DOM , DOM , ?

, DOM , jQuery. , . ready , , DOM .

, , script ?

jQuery , , ready. :

ready: function( fn ) {
    // Attach the listeners
    jQuery.bindReady(); 

    // If the DOM is already ready
    if ( jQuery.isReady ) {
        // Execute the function immediately
        fn.call( document, jQuery );

    // Otherwise, remember the function for later
    } else if ( readyList ) {
        // Add the function to the wait list
        readyList.push( fn );
    }

    return this;
},

, body, .

$({
    an: 'object', 
    that: 'has', 
    nothing: 'to', 
    'do': 'with', 
    ready: 'event'
}).ready(function() { .. });

.

(function(){});

.

, , script. , ?

, , , , DOM .

script ?

, script. , DOM. . jQuery. - DOM . JavaScript, , , , , ready(..). , "", , DOM .

<script>
    function hello() { alert("hello"); }
    hello();
</script>

JavaScript, ?

. JavaScript , . , - , , , , MVC, .

.

, - . .

<script>
    alert("Hello"), alert("World")
</script>

, .

, script , ? ?

JavaScript . , . script, . .

+13

.ready() . , $(body).ready()

, ready() 1 - .

$(document).ready(alert)

function(){}

, $(document).ready(function(){}) - .

script , , .

:

function myFunc()
{
  alert('We are in my function');
}
$(document).ready(myFunc)

:

$(document).ready(function()
{
  alert('We are in my function');
});
+4
  • (document) - . "document".
  • .ready - , jQuery. .bind("ready", f)
  • (function() { ... }) .

"" "click".

+1

jQuery, jQuery , jQuery , "" "", ?

.ready - , , DOM . "DOM" , DOM , ? , , script ? ?

. .

, , dom

$(document).ready(function(){alert('ready')})

colud alos :

$(document).ready(mxFunction)
function mxFunction(){alert('ready')}
+1

@eskimoblood " ". , , , "" jQuery , , DOM .

window.loaded , , "" "". "DOMContentLoaded", , "ready".

+1

, JavaScript jQuery, {}, (). , () {}

0
source

In jQuery, you can add a function to any jQuery event that will be fired after the event has occurred.

In this case when the (document) is ready(), run the function(){} within it.

In some cases, you can add parameters to a function. For example, when using it, jQuery.ajaxI can use the parameters in the success setting to access the data that I received from the ajax request and reuse this parameter in the function itself.

$.ajax({
  url: "test.html",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});
0
source

Source: https://habr.com/ru/post/1757166/


All Articles