jQuery syntax 


In jQuery programs, whether it is the selection of page elements or built-in function functions, it starts with the dollar sign "$", and this "$" is the most important and unique object in jQuery: jQuery object, So we can write this when the page element selects or executes a function:

$(function(){}); //Execute an anonymous function
$('#box'); //Select the ID element to execute
$('#box').css('color','red'); //Execute function function
Since "$" itself is an abbreviation for jQuery object, then the above code can be written as follows:

jQuery(function(){});
jQuery('#box');
jQuery('#box').css('color','red');


jQuery loading mode

We have been using $(function(){}) in the previous code; this code is wrapped first and last, do you know why you should do this?

The reason is that our jQuery library file is loaded before the body element, we have to wait for all the webpage code to load before we can load the JavaScript code, otherwise we can't get it. 


Instance


So our previous JavaScript gave us:

Window.onload=function(){}; //JavaScript waits for loading
Our jQuery gives us:

$(document).ready(function(){}); //jQuery waits for loading

The difference between the two:


1. Timing of execution


Window.onload : You must wait for the page to load (including the image) to execute the package code.

$(document).ready(function(){}) : Just wait for the DOM structure in the page to load and execute the package code. Higher effect

2. Number of executions


Window.onload can only be executed once, if the second time, then the first execution will be overwritten

$(document).ready(function(){}) : will not be overwritten no matter how many times it is executed

Example

3. Shorthand


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

In practical applications, we rarely use window.onload directly, because it needs to wait for large elements such as images to be loaded before executing JS code. If the network speed is slow, the page is fully expanded. The image is still loading slowly, and any JS interactions on the page are all in a suspended state, such as some drop-down menus. 

Last Updated: 2018-11-24 13:22:20 By Akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :