Home > Article > Web Front-end > There are several ways to write the entry function of jquery
There are four ways to write the entry function of jquery: 1. "$(function(){});", which is the most concise way of writing; 2. "$(document).ready(function (){});"; 3. "jquery(function(){});"; 4. "jquery(document).ready(function(){});". The function of the entry function is to wait for the DOM structure to be rendered before executing the code inside.
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
What is the entry function?
I don’t know if you still have an impression. When we were learning the BOM of native JS, because the HTML element was behind the script tag and the code was running from top to bottom, an error occurred. At that time, we There are two solutions, the first method is load, and the second method is DOMContentLoaded. This situation also exists in jQuery. If our script tag is placed in front of the HTML element, an entry function is needed
Function:
The entry function The function is to wait for the DOM structure to be rendered before executing the code inside. There is no need to wait for all resources such as images and css to be loaded. It is equivalent to DOMContentLoaded
in native JS. Writing method:
There are four ways to write the jQuery entry function. The four ways to write are shown in order by the following pictures. It is recommended that everyone use the third writing method during development. Why is it recommended that everyone use the third writing method? Because compared to the third writing method, other writing methods require more writing, and the coding efficiency is lower than the third writing method. We use jQuery to simplify JavaScript operations, allowing us to write less and do more.
1. The first type:
$(document).ready(function(){ });
##2. The second type:
jQuery(document).ready(function(){ });
3. The third type (the most concise way of writing, recommended):
$(function(){ });
4. The fourth type:
jQuery(function(){ });Video tutorial recommendation:
The above is the detailed content of There are several ways to write the entry function of jquery. For more information, please follow other related articles on the PHP Chinese website!