When newbies come into contact with JavaScript and jquery, they will inevitably come into contact with the relevant content identified by the question. After reading it several times, they are always uneasy when they are used. Write this article to remember it.
The symbol "$" is a jquery object (I understand it this way, how to use a function). Then we know the following equation, which is completely consistent in functional performance.
$(function(){ /*do some thing*/ }) = $(document).ready(function(){/* do some thing */}) = $().ready(function(){ /* do some thing */ });
The above is something in jquery. Then let’s talk about the two in native javascript, which are different from <body onload="">. There will be differences in using anonymous functions and non-anonymous functions. I can’t use them for the time being, so I haven’t done any experiments. There are many different opinions on the Internet and inconsistent. But onload is different from ready in jquery, here is:
One is different times. $().ready() can be used multiple times in one html, onload can only be used once or the valid binding can only be the only one.
Another is the difference in execution timing. Simply put, the execution time of $() should be earlier than onload, because $() will be executed as long as it is loaded in the DOM tree, and onload must be completed in the loading of all elements.
The above is the difference between $(function) ready and onload introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for my website!