$ (Function () {}) in javascript; What are the differences between writing and not writing?

Source: Internet
Author: User
$ (Function () {...}) in javascript (){....}) is a classic usage in jQuery, equivalent to $ (document ). ready (function (){....}) $ (function () {}) in javascript; What are the differences between writing and not writing? For a friend in need, refer to $ (function () {...}) in javascript (){....}) is a classic usage in jQuery, equivalent to $ (document ). ready (function (){....}), that is to say, a function is executed only after the page is loaded. If you want to operate DOM in the function, it is safer to execute the function after the page is loaded. Therefore, this method is common when using jQuery.

$ (Document ). the code in ready () is executed only after the page content is loaded. If you directly write the code to the script tag, after the script tag is loaded on the page, the code in it will be executed. If the code executed in your tag calls the code or dom that has not been loaded yet, an error will be reported, of course, if you put the script tag at the end of the page, there will be no problem. This is the same as ready.

$ (Document). ready (function () {}) can be abbreviated to $ (function (){});

After clicking a paragraph, this paragraph is hidden:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.