开发者

What does putting a javascript function inside a '$()' do?

开发者 https://www.devze.com 2023-02-09 18:28 出处:网络
I\'m learnin开发者_开发知识库g some AJAX right now and the jQuery function that is being used to submit the form is wrapped inside $( function() { } ) as so. What does this exactly do?

I'm learnin开发者_开发知识库g some AJAX right now and the jQuery function that is being used to submit the form is wrapped inside $( function() { } ) as so. What does this exactly do?

$(function() {
    $('.error').hide();
    $(".button").click(function() {
        // validate and process form here
    }
});


$(function() { }); is short-hand for $(document).ready(function() { });

See documentation.


This is a shortcut provided by jQuery for running code on page ready. It is equivalent to:

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

jQuery will call this function when the page is ready to be manipulated.

Docs


This is the same thing as $(document).ready(). It just a shortcut to do $(function(){...}) instead of using the ready function.


$(function() { }) Waits for the dokument to load before doing enything to the dokument. same as $(document).ready(function() { });


As others have stated, it's a shortcut for jQuery(document).ready(fn), which is a cross-browser implementation of document.addEventListener('DOMContentLoaded', fn, useCapture)

https://developer.mozilla.org/en/Gecko-Specific_DOM_Events

Fired at the page's Document object when parsing of the document is finished. By the time this event fires, the page's DOM is ready, but the referenced stylesheets, images, and subframes may not be done loading; use the "load" event to detect a fully-loaded page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号