Home >Web Front-end >Front-end Q&A >There are several ways to write the entry function of jquery

There are several ways to write the entry function of jquery

WBOY
WBOYOriginal
2022-06-08 10:38:012981browse

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.

There are several ways to write the entry function of jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

jquery’s entry function

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(){ 
});

There are several ways to write the entry function of jquery

##2. The second type:

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

There are several ways to write the entry function of jquery

3. The third type (the most concise way of writing, recommended):

$(function(){
 });

There are several ways to write the entry function of jquery

4. The fourth type:

jQuery(function(){ 
});

There are several ways to write the entry function of jquery

Video tutorial recommendation:

jQuery video tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn